Changeset View
Changeset View
Standalone View
Standalone View
src/dht/tests/keytest.cpp
Context not available. | |||||
43 | { | 43 | { | ||
---|---|---|---|---|---|
44 | bt::Uint8 result[20]; | 44 | bt::Uint8 result[20]; | ||
45 | std::fill(result, result + 20, 0); | 45 | std::fill(result, result + 20, 0); | ||
46 | 46 | | |||
47 | QString s = str.toLower(); | 47 | QString s = str.toLower(); | ||
48 | if (s.size() % 2 != 0) | 48 | if (s.size() % 2 != 0) | ||
49 | s.prepend('0'); | 49 | s.prepend('0'); | ||
50 | 50 | | |||
51 | int j = 19; | 51 | int j = 19; | ||
52 | 52 | | |||
53 | for (int i = s.size() - 1; i >= 0; i -= 2) | 53 | for (int i = s.size() - 1; i >= 0; i -= 2) | ||
54 | { | 54 | { | ||
55 | char left = s[i - 1].toLatin1(); | 55 | char left = s[i - 1].toLatin1(); | ||
56 | char right = s[i].toLatin1(); | 56 | char right = s[i].toLatin1(); | ||
57 | result[j--] = (HexCharToUint8(left) << 4) | HexCharToUint8(right); | 57 | result[j--] = (HexCharToUint8(left) << 4) | HexCharToUint8(right); | ||
58 | } | 58 | } | ||
59 | 59 | | |||
60 | return dht::Key(result); | 60 | return dht::Key(result); | ||
61 | } | 61 | } | ||
62 | 62 | | |||
Context not available. | |||||
68 | { | 68 | { | ||
69 | bt::InitLog("keytest.log", false, true); | 69 | bt::InitLog("keytest.log", false, true); | ||
70 | } | 70 | } | ||
71 | 71 | | |||
72 | void cleanupTestCase() | 72 | void cleanupTestCase() | ||
73 | { | 73 | { | ||
74 | } | 74 | } | ||
75 | 75 | | |||
76 | void testAddition() | 76 | void testAddition() | ||
77 | { | 77 | { | ||
78 | dht::Key a = KeyFromHexString("14455"); | 78 | dht::Key a = KeyFromHexString("14455"); | ||
79 | dht::Key b = KeyFromHexString("FFEEDD"); | 79 | dht::Key b = KeyFromHexString("FFEEDD"); | ||
80 | dht::Key c = a + b; | 80 | dht::Key c = a + b; | ||
81 | QVERIFY(c == KeyFromHexString("1013332")); | 81 | QVERIFY(c == KeyFromHexString("1013332")); | ||
82 | 82 | | |||
83 | dht::Key d = a + 1; | 83 | dht::Key d = a + 1; | ||
84 | QVERIFY(d == KeyFromHexString("14456")); | 84 | QVERIFY(d == KeyFromHexString("14456")); | ||
85 | 85 | | |||
86 | dht::Key e = KeyFromHexString("FFFF") + 1; | 86 | dht::Key e = KeyFromHexString("FFFF") + 1; | ||
87 | QVERIFY(e == KeyFromHexString("10000")); | 87 | QVERIFY(e == KeyFromHexString("10000")); | ||
88 | | ||||
89 | dht::Key f = KeyFromHexString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF") + 1; | ||||
90 | QVERIFY(f == KeyFromHexString("0000000000000000000000000000000000000000")); | ||||
91 | | ||||
92 | dht::Key h = KeyFromHexString("FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF"); | ||||
93 | dht::Key j = KeyFromHexString("0000000100000000000000010000000000000001"); | ||||
94 | dht::Key g = j + h; | ||||
95 | QVERIFY(g == KeyFromHexString("0000000000000001000000000000000100000000")); | ||||
88 | } | 96 | } | ||
89 | 97 | | |||
90 | void testSubtraction() | 98 | void testSubtraction() | ||
91 | { | 99 | { | ||
92 | dht::Key a = KeyFromHexString("556677"); | 100 | dht::Key a = KeyFromHexString("556677"); | ||
93 | dht::Key b = KeyFromHexString("3384E6"); | 101 | dht::Key b = KeyFromHexString("3384E6"); | ||
94 | dht::Key c = a - b; | 102 | dht::Key c = a - b; | ||
95 | QVERIFY(c == KeyFromHexString("21E191")); | 103 | QVERIFY(c == KeyFromHexString("21E191")); | ||
96 | 104 | | |||
97 | dht::Key d = KeyFromHexString("550077"); | 105 | dht::Key d = KeyFromHexString("550077"); | ||
98 | dht::Key e = KeyFromHexString("3384E6"); | 106 | dht::Key e = KeyFromHexString("3384E6"); | ||
99 | dht::Key f = d - e; | 107 | dht::Key f = d - e; | ||
100 | QVERIFY(f == KeyFromHexString("217B91")); | 108 | QVERIFY(f == KeyFromHexString("217B91")); | ||
109 | | ||||
110 | f = e - d; | ||||
111 | QVERIFY(f == KeyFromHexString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDE846F")); | ||||
112 | | ||||
113 | dht::Key g = KeyFromHexString("0000000000000001000000000000000100000000"); | ||||
114 | dht::Key j = KeyFromHexString("0000000100000000000000010000000000000001"); | ||||
115 | dht::Key h = g - j; | ||||
116 | QVERIFY(h == KeyFromHexString("FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF")); | ||||
117 | | ||||
101 | } | 118 | } | ||
102 | 119 | | |||
103 | void testDivision() | 120 | void testDivision() | ||
104 | { | 121 | { | ||
105 | dht::Key a = KeyFromHexString("550078"); | 122 | dht::Key a = KeyFromHexString("550078"); | ||
106 | dht::Key b = a / 2; | 123 | dht::Key b = a / 2; | ||
107 | QVERIFY(b == KeyFromHexString("2A803C")); | 124 | QVERIFY(b == KeyFromHexString("2A803C")); | ||
125 | | ||||
126 | dht::Key c = KeyFromHexString("0000000100000000000000010000000000000001"); | ||||
127 | dht::Key d = c / 32; | ||||
128 | QVERIFY(d == KeyFromHexString("0000000008000000000000000800000000000000")); | ||||
129 | | ||||
108 | } | 130 | } | ||
109 | }; | 131 | }; | ||
110 | 132 | | |||
Context not available. |