BencodeVisitorTest.cc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "BencodeVisitor.h"
  2. #include "Data.h"
  3. #include "List.h"
  4. #include "Dictionary.h"
  5. #include <cppunit/extensions/HelperMacros.h>
  6. namespace aria2 {
  7. class BencodeVisitorTest:public CppUnit::TestFixture {
  8. CPPUNIT_TEST_SUITE(BencodeVisitorTest);
  9. CPPUNIT_TEST(testVisit_data);
  10. CPPUNIT_TEST(testVisit_list);
  11. CPPUNIT_TEST(testVisit_dictionary);
  12. CPPUNIT_TEST_SUITE_END();
  13. private:
  14. public:
  15. void setUp() {
  16. }
  17. void testVisit_data();
  18. void testVisit_list();
  19. void testVisit_dictionary();
  20. };
  21. CPPUNIT_TEST_SUITE_REGISTRATION( BencodeVisitorTest );
  22. void BencodeVisitorTest::testVisit_data()
  23. {
  24. {
  25. BencodeVisitor v;
  26. std::string str = "apple";
  27. MetaEntryHandle m(new Data(str.c_str(), str.size()));
  28. m->accept(&v);
  29. CPPUNIT_ASSERT_EQUAL(std::string("5:apple"), v.getBencodedData());
  30. }
  31. {
  32. BencodeVisitor v;
  33. std::string str = "123";
  34. MetaEntryHandle m(new Data(str.c_str(), str.size(), true));
  35. m->accept(&v);
  36. CPPUNIT_ASSERT_EQUAL(std::string("i123e"), v.getBencodedData());
  37. }
  38. }
  39. void BencodeVisitorTest::testVisit_list()
  40. {
  41. BencodeVisitor v;
  42. List l;
  43. std::string s1 = "alpha";
  44. l.add(new Data(s1.c_str(), s1.size()));
  45. std::string s2 = "bravo";
  46. l.add(new Data(s2.c_str(), s2.size()));
  47. std::string s3 = "123";
  48. l.add(new Data(s3.c_str(), s3.size(), true));
  49. l.accept(&v);
  50. CPPUNIT_ASSERT_EQUAL(std::string("l5:alpha5:bravoi123ee"), v.getBencodedData());
  51. }
  52. void BencodeVisitorTest::testVisit_dictionary()
  53. {
  54. BencodeVisitor v;
  55. Dictionary d;
  56. std::string s1 = "alpha";
  57. d.put("team", new Data(s1.c_str(), s1.size()));
  58. std::string s2 = "123";
  59. d.put("score", new Data(s2.c_str(), s2.size(), true));
  60. d.accept(&v);
  61. CPPUNIT_ASSERT_EQUAL(std::string("d4:team5:alpha5:scorei123ee"), v.getBencodedData());
  62. }
  63. } // namespace aria2