1 #include "SegList.h"
2
3 #include <cppunit/extensions/HelperMacros.h>
4
5 namespace aria2 {
6
7 class SegListTest : public CppUnit::TestFixture {
8
9 CPPUNIT_TEST_SUITE(SegListTest);
10 CPPUNIT_TEST(testNext);
11 CPPUNIT_TEST(testPeek);
12 CPPUNIT_TEST(testClear);
13 CPPUNIT_TEST(testNormalize);
14 CPPUNIT_TEST_SUITE_END();
15
16 public:
17 void testNext();
18 void testPeek();
19 void testClear();
20 void testNormalize();
21 };
22
23 CPPUNIT_TEST_SUITE_REGISTRATION(SegListTest);
24
testNext()25 void SegListTest::testNext()
26 {
27 SegList<int> sgl;
28 sgl.add(-500, -498);
29 sgl.add(5, 10);
30 sgl.add(1, 5);
31 for (int i = -500; i < -498; ++i) {
32 CPPUNIT_ASSERT(sgl.hasNext());
33 CPPUNIT_ASSERT_EQUAL(i, sgl.next());
34 }
35 for (int i = 5; i < 10; ++i) {
36 CPPUNIT_ASSERT(sgl.hasNext());
37 CPPUNIT_ASSERT_EQUAL(i, sgl.next());
38 }
39 for (int i = 1; i < 5; ++i) {
40 CPPUNIT_ASSERT(sgl.hasNext());
41 CPPUNIT_ASSERT_EQUAL(i, sgl.next());
42 }
43 CPPUNIT_ASSERT(!sgl.hasNext());
44 CPPUNIT_ASSERT_EQUAL(0, sgl.next());
45 }
46
testPeek()47 void SegListTest::testPeek()
48 {
49 SegList<int> sgl;
50 sgl.add(1, 3);
51 sgl.add(4, 5);
52 CPPUNIT_ASSERT_EQUAL(1, sgl.peek());
53 CPPUNIT_ASSERT_EQUAL(1, sgl.peek());
54 CPPUNIT_ASSERT_EQUAL(1, sgl.next());
55 CPPUNIT_ASSERT_EQUAL(2, sgl.peek());
56 CPPUNIT_ASSERT_EQUAL(2, sgl.next());
57 CPPUNIT_ASSERT_EQUAL(4, sgl.peek());
58 CPPUNIT_ASSERT_EQUAL(4, sgl.next());
59 CPPUNIT_ASSERT(!sgl.hasNext());
60 }
61
testClear()62 void SegListTest::testClear()
63 {
64 SegList<int> sgl;
65 sgl.add(1, 3);
66 CPPUNIT_ASSERT_EQUAL(1, sgl.next());
67 sgl.clear();
68 CPPUNIT_ASSERT(!sgl.hasNext());
69 sgl.add(2, 3);
70 CPPUNIT_ASSERT_EQUAL(2, sgl.next());
71 }
72
testNormalize()73 void SegListTest::testNormalize()
74 {
75 SegList<int> sgl;
76 sgl.add(10, 15);
77 sgl.add(0, 1);
78 sgl.add(1, 5);
79 sgl.add(14, 16);
80 sgl.add(2, 4);
81 sgl.add(20, 21);
82 sgl.normalize();
83 for (int i = 0; i < 5; ++i) {
84 CPPUNIT_ASSERT(sgl.hasNext());
85 CPPUNIT_ASSERT_EQUAL(i, sgl.next());
86 }
87 for (int i = 10; i < 16; ++i) {
88 CPPUNIT_ASSERT(sgl.hasNext());
89 CPPUNIT_ASSERT_EQUAL(i, sgl.next());
90 }
91 CPPUNIT_ASSERT(sgl.hasNext());
92 CPPUNIT_ASSERT_EQUAL(20, sgl.next());
93 CPPUNIT_ASSERT(!sgl.hasNext());
94 }
95
96 } // namespace aria2
97