XORCloserTest.cc 1.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#include "XORCloser.h"
#include "Exception.h"
#include "util.h"
#include "DHTNodeLookupEntry.h"
#include "DHTNode.h"
#include <cstring>
#include <algorithm>
#include <cppunit/extensions/HelperMacros.h>

namespace aria2 {

12
class XORCloserTest : public CppUnit::TestFixture {
13 14 15 16

  CPPUNIT_TEST_SUITE(XORCloserTest);
  CPPUNIT_TEST(testOperator);
  CPPUNIT_TEST_SUITE_END();
17

18 19 20 21 22 23 24 25 26 27 28 29 30 31
public:
  void setUp() {}

  void tearDown() {}

  void testOperator();
};

CPPUNIT_TEST_SUITE_REGISTRATION(XORCloserTest);

void XORCloserTest::testOperator()
{
  const size_t NUM_KEY = 6;
  unsigned char keys[NUM_KEY][DHT_ID_LENGTH];
32
  memset(keys, 0, 6 * DHT_ID_LENGTH);
33 34 35 36 37 38

  keys[0][0] = 0xf0;
  keys[1][0] = 0xb0;
  keys[2][0] = 0xa0;
  keys[3][0] = 0x80;
  keys[4][0] = 0x00;
39
  keys[4][DHT_ID_LENGTH - 1] = 0x01;
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  keys[5][0] = 0x00;

  std::deque<unsigned char*> l(&keys[0], &keys[NUM_KEY]);

  std::sort(l.begin(), l.end(), XORCloser(keys[2], DHT_ID_LENGTH));

  CPPUNIT_ASSERT(memcmp(keys[2], l[0], DHT_ID_LENGTH) == 0);
  CPPUNIT_ASSERT(memcmp(keys[1], l[1], DHT_ID_LENGTH) == 0);
  CPPUNIT_ASSERT(memcmp(keys[3], l[2], DHT_ID_LENGTH) == 0);
  CPPUNIT_ASSERT(memcmp(keys[0], l[3], DHT_ID_LENGTH) == 0);
  CPPUNIT_ASSERT(memcmp(keys[5], l[4], DHT_ID_LENGTH) == 0);
  CPPUNIT_ASSERT(memcmp(keys[4], l[5], DHT_ID_LENGTH) == 0);
}

} // namespace aria2