//-----------------------------------------------------------------------------
// Project     : VST SDK
//
// Category    : Helpers
// Filename    : public.sdk/source/vst/hosting/test/eventlisttest.cpp
// Created by  : Steinberg, 08/2021
// Description : Test event list
// Flags       : clang-format SMTGSequencer
//
//-----------------------------------------------------------------------------
// This file is part of a Steinberg SDK. It is subject to the license terms
// in the LICENSE file found in the top-level directory of this distribution
// and at www.steinberg.net/sdklicenses. 
// No part of the SDK, including this file, may be copied, modified, propagated,
// or distributed except according to the terms contained in the LICENSE file.
//-----------------------------------------------------------------------------

#include "public.sdk/source/main/moduleinit.h"
#include "public.sdk/source/vst/hosting/eventlist.h"
#include "public.sdk/source/vst/utility/testing.h"

//------------------------------------------------------------------------
namespace Steinberg {
namespace Vst {
namespace {

//------------------------------------------------------------------------
ModuleInitializer EventListTests ([] () {
	constexpr auto TestSuiteName = "EventList";
	registerTest (TestSuiteName, STR ("Set and get single event"), [] (ITestResult* testResult) {
		EventList eventList;
		Event event1 = {};
		event1.type = Event::kNoteOnEvent;
		event1.noteOn.noteId = 10;
		EXPECT_EQ (eventList.addEvent (event1), kResultTrue);
		Event event2;
		EXPECT_EQ (eventList.getEvent (0, event2), kResultTrue);
		EXPECT_EQ (memcmp (&event1, &event2, sizeof (Event)), 0);
		return true;
	});
	registerTest (TestSuiteName, STR ("Count events"), [] (ITestResult* testResult) {
		EventList eventList;
		Event event = {};
		for (auto i = 0; i < 20; ++i)
		{
			EXPECT_EQ (eventList.addEvent (event), kResultTrue);
		}
		EXPECT_EQ (eventList.getEventCount (), 20);
		return true;
	});
	registerTest (TestSuiteName, STR ("Overflow"), [] (ITestResult* testResult) {
		EventList eventList (20);
		Event event = {};
		for (auto i = 0; i < 20; ++i)
		{
			EXPECT_EQ (eventList.addEvent (event), kResultTrue);
		}
		EXPECT_EQ (eventList.getEventCount (), 20);
		EXPECT_NE (eventList.addEvent (event), kResultTrue);
		EXPECT_EQ (eventList.getEventCount (), 20);
		return true;
	});
	registerTest (TestSuiteName, STR ("Get unknown event"), [] (ITestResult* testResult) {
		EventList eventList;
		Event event {};
		EXPECT_NE (eventList.getEvent (0, event), kResultTrue);
		return true;
	});
	registerTest (TestSuiteName, STR ("Resize"), [] (ITestResult* testResult) {
		EventList eventList (1);
		Event event {};
		EXPECT_NE (eventList.getEvent (0, event), kResultTrue);
		event = {};
		EXPECT_EQ (eventList.addEvent (event), kResultTrue);
		EXPECT_EQ (eventList.getEventCount (), 1);
		EXPECT_NE (eventList.addEvent (event), kResultTrue);
		EXPECT_EQ (eventList.getEventCount (), 1);
		eventList.setMaxSize (2);
		EXPECT_EQ (eventList.getEventCount (), 0);
		EXPECT_EQ (eventList.addEvent (event), kResultTrue);
		EXPECT_EQ (eventList.addEvent (event), kResultTrue);
		EXPECT_EQ (eventList.getEventCount (), 2);
		EXPECT_NE (eventList.addEvent (event), kResultTrue);
		EXPECT_EQ (eventList.getEventCount (), 2);
		return true;
	});
});

//------------------------------------------------------------------------
} // anonymous
} // Vst
} // Steinberg
