BencodeVisitorTest.cc 1.7 KB

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