SegListTest.cc 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "SegList.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. namespace aria2 {
  4. class SegListTest:public CppUnit::TestFixture {
  5. CPPUNIT_TEST_SUITE(SegListTest);
  6. CPPUNIT_TEST(testNext);
  7. CPPUNIT_TEST(testNormalize);
  8. CPPUNIT_TEST_SUITE_END();
  9. public:
  10. void testNext();
  11. void testNormalize();
  12. };
  13. CPPUNIT_TEST_SUITE_REGISTRATION(SegListTest);
  14. void SegListTest::testNext()
  15. {
  16. SegList<int> sgl;
  17. sgl.add(-500, -498);
  18. sgl.add(5, 10);
  19. sgl.add(1, 5);
  20. for(int i = -500; i < -498; ++i) {
  21. CPPUNIT_ASSERT(sgl.hasNext());
  22. CPPUNIT_ASSERT_EQUAL(i, sgl.next());
  23. }
  24. for(int i = 5; i < 10; ++i) {
  25. CPPUNIT_ASSERT(sgl.hasNext());
  26. CPPUNIT_ASSERT_EQUAL(i, sgl.next());
  27. }
  28. for(int i = 1; i < 5; ++i) {
  29. CPPUNIT_ASSERT(sgl.hasNext());
  30. CPPUNIT_ASSERT_EQUAL(i, sgl.next());
  31. }
  32. CPPUNIT_ASSERT(!sgl.hasNext());
  33. CPPUNIT_ASSERT_EQUAL(0, sgl.next());
  34. }
  35. void SegListTest::testNormalize()
  36. {
  37. SegList<int> sgl;
  38. sgl.add(10, 15);
  39. sgl.add(0, 1);
  40. sgl.add(1, 5);
  41. sgl.add(14, 16);
  42. sgl.add(2, 4);
  43. sgl.add(20, 21);
  44. sgl.normalize();
  45. for(int i = 0; i < 5; ++i) {
  46. CPPUNIT_ASSERT(sgl.hasNext());
  47. CPPUNIT_ASSERT_EQUAL(i, sgl.next());
  48. }
  49. for(int i = 10; i < 16; ++i) {
  50. CPPUNIT_ASSERT(sgl.hasNext());
  51. CPPUNIT_ASSERT_EQUAL(i, sgl.next());
  52. }
  53. CPPUNIT_ASSERT(sgl.hasNext());
  54. CPPUNIT_ASSERT_EQUAL(20, sgl.next());
  55. CPPUNIT_ASSERT(!sgl.hasNext());
  56. }
  57. } // namespace aria2