File: TestsStlVector.cxx

package info (click to toggle)
clam 1.4.0-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,836 kB
  • ctags: 20,981
  • sloc: cpp: 92,504; python: 9,721; ansic: 1,602; xml: 444; sh: 239; makefile: 153; perl: 54; asm: 15
file content (119 lines) | stat: -rw-r--r-- 2,451 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

#include <cppunit/extensions/HelperMacros.h>
#include <vector>
#include <list>
//#include <deque> VC7.1 compiler chokes on that.


namespace CLAMTest {

class TestsStlVector ;
CPPUNIT_TEST_SUITE_REGISTRATION( TestsStlVector );

class TestsStlVector : public CppUnit::TestFixture
{
public:
	CPPUNIT_TEST_SUITE( TestsStlVector );

	CPPUNIT_TEST( testReserve );
	CPPUNIT_TEST( testAt );
	CPPUNIT_TEST( testResize_beforeReserve );
	CPPUNIT_TEST( testList_isCircularWithPhantomElement );
	CPPUNIT_TEST( testDeque_elementsNotInContiguousSpace );
	CPPUNIT_TEST( testVectorInsertInTheMiddle );

	CPPUNIT_TEST_SUITE_END();

	void testReserve()
	{
		std::vector<int> vector;
		vector.reserve(5);
		CPPUNIT_ASSERT(0 == vector.size());

	}
	void testAt()
	{
		std::vector<int> vector;
		vector.reserve(5);
		try {
			vector.at(1); // en canvi l'operator[] no fa comprovacions
			CPPUNIT_FAIL("hauria d'haver llenat exception"); //funciona tant en debug com en release
		} catch (std::exception&)
		{
		}
	}

	void testResize_beforeReserve()
	{
		std::vector<int> vector;
		vector.resize(5);
		CPPUNIT_ASSERT(5 == vector.size());
		CPPUNIT_ASSERT(5 <= vector.capacity());

		vector.at(4)=1;
		CPPUNIT_ASSERT(1==vector.at(4));
	}

	void testList_isCircularWithPhantomElement()
	{
		typedef std::list<int> List;
		List list;
		list.push_back(0);
		list.push_back(1);
		list.push_back(2);

		List::iterator it=list.begin();
		for(int i=0; i<3; i++,it++);

		CPPUNIT_ASSERT(it==list.end());

		it++; // we are out of range: let's see that magically we're in the beginning
		CPPUNIT_ASSERT(it!=list.end());
		(*it)=66;
		CPPUNIT_ASSERT(66==*list.begin());

	}

	void testDeque_elementsNotInContiguousSpace()
	{
/*		VC7 Compiler problem with deque
 
		std::deque<int> deque;
		
		deque.push_back(1);
		deque.push_back(2);
		deque.push_back(3);
		deque.push_back(4);
		deque.push_back(5);
		deque.push_front(0);

		CPPUNIT_ASSERT_EQUAL(5, deque.at(5));
		CPPUNIT_ASSERT(5 != *(&(deque.at(0))+5) );
*/
}

	void testVectorInsertInTheMiddle()
	{
		std::vector<char> buff;
		buff.resize(8);
		buff[0]='h';
		buff[1]='e';
		buff[2]='o';
		buff[3]=' ';
		buff[4]='a';
		buff[5]='l';
		buff[6]='l';
		buff[7]='\0';
		std::vector<char>::iterator it  = buff.begin();
		it += 2;
		buff.insert(it, 2, 'l');

		CPPUNIT_ASSERT_EQUAL( 10, int(buff.size()) );
		CPPUNIT_ASSERT_EQUAL( std::string("hello all"), 
				std::string((char *)(&buff[0])) );
	}
};


} // namespace CLAMTest