#include <sys/time.h>
#include <coral/core/Element.h>
#include <coral/coral.h>
#include <coral/core/garbage_collector.h>

#include <iostream>

#include <unistd.h>

int main() {
	coral::init_coral_framework();
	coral::autounref<coral::Language> uml14
		= coral::Language::__getLanguage__("UML", 1, 4);
	coral_assert(uml14.get());

	int i = coral::all_modelelements_size();

	std::cout << "Going to create 10000 elements" << std::endl << std::endl;
	coral::Atom* _m = uml14->getElement("Model")->newAtom();
	coral::Element* m = static_cast<coral::Element*>(_m);


	struct timeval start_time;
	struct timeval mid_time;
	struct timeval end_time;

	gettimeofday(&start_time, NULL);


	for (int i = 0; i < 10000; ++i) {
		coral::Atom* _c = uml14->getElement("Class")->newAtom();
		coral::Element* c = static_cast<coral::Element*>(_c);
		m->__getSlotByName__("ownedElement").append(c);
		c->unref();
	}

	m->unref();

	coral_assert(coral::all_modelelements_size() > i);

	gettimeofday(&mid_time, NULL);

	coral::garbage_collect();

	gettimeofday(&end_time, NULL);

	coral_assert(coral::all_modelelements_size() == i);
	std::cout << "Tada! It deleted the elements!" << std::endl;

	printf("Init:   %d.%d\n", mid_time.tv_sec - start_time.tv_sec, mid_time.tv_usec - start_time.tv_usec);
	printf("Delete: %d.%d\n", end_time.tv_sec - mid_time.tv_sec, end_time.tv_usec - mid_time.tv_usec);

	return 0;
}
