Skip to content
Snippets Groups Projects
LinearSetTest.cpp 2.45 KiB
Newer Older
Jan Trávníček's avatar
Jan Trávníček committed
#include "LinearSetTest.h"
#include <linear_set>
#include <algorithm>

CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LinearSetTest, "bits" );
CPPUNIT_TEST_SUITE_REGISTRATION( LinearSetTest );

void LinearSetTest::setUp() {
}

void LinearSetTest::tearDown() {
}

void LinearSetTest::test1() {
	std::vector < int > data = { 1, 4, 3 };
	std::linear_set < int > test1;
	std::linear_set < int > test2 ( data.begin ( ), data.end ( ) );

	CPPUNIT_ASSERT ( test1.size ( ) == 0 );
	CPPUNIT_ASSERT ( test1.empty ( ) );
	CPPUNIT_ASSERT ( test2.size ( ) == 3 );

	test1.insert ( 1 );
	CPPUNIT_ASSERT ( test1.size ( ) == 1 );
	CPPUNIT_ASSERT ( ! test1.empty ( ) );
	for ( int elem : test1 ) {
		CPPUNIT_ASSERT ( elem == 1 );
	}
	test1.clear ( );
	CPPUNIT_ASSERT ( test1.empty ( ) );
	std::linear_set < int >::iterator iter = test1.insert ( data [0] ).first;
	CPPUNIT_ASSERT ( iter == test1.begin ( ) );

	iter = test1.insert ( data [1] ).first;
	CPPUNIT_ASSERT ( iter == test1.begin ( ) + 1 );

	iter = test1.insert ( data [2] ).first;
	CPPUNIT_ASSERT ( iter == test1.begin ( ) + 1 );

	CPPUNIT_ASSERT ( test1 == test2 );
	CPPUNIT_ASSERT ( ! test1.insert ( data [0] ).second );
	iter = test1.insert ( data [0] ).first;
	CPPUNIT_ASSERT ( iter == test1.begin ( ) );

	test1.insert ( { 2, 3 } );
	CPPUNIT_ASSERT ( test1.size ( ) == 4 );

	std::vector < int > ref = { 1, 2, 3, 4 };
	std::vector < int > copy ( test1.begin ( ), test1.end ( ) );
	CPPUNIT_ASSERT ( ref == copy );

	ref = { 4, 3, 2, 1 };
	copy = std::vector < int > ( test1.rbegin ( ), test1.rend ( ) );
	CPPUNIT_ASSERT ( ref == copy );
}

void LinearSetTest::test2() {
	std::linear_set<int> first = {1};
	std::linear_set<int> second = {1, 2, 3};

	std::linear_set<int> firstMinusSecond;
	std::linear_set<int> secondMinusFirst;

	std::set_difference (first.begin(), first.end(), second.begin(), second.end(), std::inserter(firstMinusSecond, firstMinusSecond.end()));
	std::set_difference (second.begin(), second.end(), first.begin(), first.end(), std::inserter(secondMinusFirst, secondMinusFirst.end()));

	CPPUNIT_ASSERT_EQUAL(firstMinusSecond.size(), (size_t) 0u);
	CPPUNIT_ASSERT_EQUAL(secondMinusFirst.size(), (size_t) 2u);
}

void LinearSetTest::test3() {
	int moves;
	int copies;

	std::linear_set<LinearSetTest::Moveable> set;
	set.insert ( LinearSetTest::Moveable(moves, copies) );
	std::linear_set<LinearSetTest::Moveable> set2;

	for(Moveable moveable : std::make_moveable_set(set)) {
		set2.insert(std::move(moveable));
	}

	CPPUNIT_ASSERT(copies == 0);
}