Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Working on 32 bit support.

  • Loading branch information...
commit 4109fc68d9f59456963892578b3d660601424da0 1 parent fc91c35
Caleb James DeLisle authored
Showing with 25 additions and 24 deletions.
  1. +4 −5 dht/dhtcore/VersionList.c
  2. +21 −19 dht/dhtcore/test/VersionList_test.c
View
9 dht/dhtcore/VersionList.c
@@ -47,12 +47,12 @@ struct VersionList* VersionList_parse(String* str, struct Allocator* alloc)
String* VersionList_stringify(struct VersionList* list, struct Allocator* alloc)
{
- uint8_t numberSize = 0;
- uint32_t max = 0;
+ uint8_t numberSize = 1;
+ uint32_t max = 0xff;
for (int i = 0; i < (int)list->length; i++) {
- if (list->versions[i] >= max) {
+ while (list->versions[i] >= max) {
numberSize++;
- max = UINT32_MAX >> (32 - (numberSize * 8));
+ max = max << 8 | 0xff;
}
}
@@ -67,7 +67,6 @@ printf("writing [%d]\n", list->versions[i]);
ver = Endian_hostToBigEndian32(ver);
Writer_write(w, (uint8_t*) &ver, numberSize);
}
- Writer_write(w, &numberSize, 1);
return out;
}
View
40 dht/dhtcore/test/VersionList_test.c
@@ -24,29 +24,31 @@ int main()
struct Allocator* alloc = CanaryAllocator_new(MallocAllocator_new(1<<20), NULL);
struct Random* rand = Random_new(alloc, NULL, NULL);
- for (int bytes = 1; bytes < 5; bytes++) {
- uint32_t count = Random_uint32(rand) % 30;
- uint32_t max = Random_uint32(rand) % UINT32_MAX >> ((4-bytes) * 8);
- if (max < 2) {
- max += 2;
- }
- struct VersionList* vl = VersionList_new(count, alloc);
- for (uint32_t i = 0; i < count; i++) {
- vl->versions[i] = Random_uint32(rand) % max;
- }
+ for (int cycles = 0; cycles < 10000; cycles++) {
+ for (int bytes = 1; bytes < 5; bytes++) {
+ uint32_t count = Random_uint32(rand) % 30;
+ uint32_t max = Random_uint32(rand) % UINT32_MAX >> ((4-bytes) * 8);
+ if (max < 2) {
+ max += 2;
+ }
+ struct VersionList* vl = VersionList_new(count, alloc);
+ for (uint32_t i = 0; i < count; i++) {
+ vl->versions[i] = Random_uint32(rand) % max;
+ }
- String* str = VersionList_stringify(vl, alloc);
+ String* str = VersionList_stringify(vl, alloc);
- uint8_t* buff = Allocator_malloc(alloc, (str->len+1) * 2 + 1);
- Hex_encode(buff, (str->len+1) * 2 + 1, (uint8_t*)str->bytes, str->len+1);
- printf("Got versions [%s]\n", buff);
+ uint8_t* buff = Allocator_malloc(alloc, (str->len+1) * 2 + 1);
+ Hex_encode(buff, (str->len+1) * 2 + 1, (uint8_t*)str->bytes, str->len+1);
+ printf("Got versions [%s]\n", buff);
- struct VersionList* vl2 = VersionList_parse(str, alloc);
+ struct VersionList* vl2 = VersionList_parse(str, alloc);
- Assert_always(vl->length == vl2->length && vl->length == count);
- for (uint32_t i = 0; i < count; i++) {
- printf("[%d] [%d]\n", vl2->versions[i], vl->versions[i]);
- Assert_always(vl2->versions[i] == vl->versions[i]);
+ Assert_always(vl->length == vl2->length && vl->length == count);
+ for (uint32_t i = 0; i < count; i++) {
+ printf("[%d] [%d]\n", vl2->versions[i], vl->versions[i]);
+ Assert_always(vl2->versions[i] == vl->versions[i]);
+ }
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.