host/storage: use fail/pass api in unit tests

pull/15119/head
Jason Ish 4 months ago committed by Victor Julien
parent 31a4381d30
commit 332f47d557

@ -127,18 +127,14 @@ static int HostStorageTest01(void)
StorageInit(); StorageInit();
HostStorageId id1 = HostStorageRegister("test", 8, StorageTestAlloc, StorageTestFree); HostStorageId id1 = HostStorageRegister("test", 8, StorageTestAlloc, StorageTestFree);
if (id1.id < 0) FAIL_IF(id1.id < 0);
goto error;
HostStorageId id2 = HostStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree); HostStorageId id2 = HostStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree);
if (id2.id < 0) FAIL_IF(id2.id < 0);
goto error;
HostStorageId id3 = HostStorageId id3 =
HostStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree); HostStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
if (id3.id < 0) FAIL_IF(id3.id < 0);
goto error;
if (StorageFinalize() < 0) FAIL_IF(StorageFinalize() < 0);
goto error;
HostInitConfig(1); HostInitConfig(1);
@ -147,59 +143,34 @@ static int HostStorageTest01(void)
a.addr_data32[0] = 0x01020304; a.addr_data32[0] = 0x01020304;
a.family = AF_INET; a.family = AF_INET;
Host *h = HostGetHostFromHash(&a); Host *h = HostGetHostFromHash(&a);
if (h == NULL) { FAIL_IF_NULL(h);
printf("failed to get host: ");
goto error;
}
void *ptr = HostGetStorageById(h, id1); void *ptr = HostGetStorageById(h, id1);
if (ptr != NULL) { FAIL_IF_NOT_NULL(ptr);
goto error;
}
ptr = HostGetStorageById(h, id2); ptr = HostGetStorageById(h, id2);
if (ptr != NULL) { FAIL_IF_NOT_NULL(ptr);
goto error;
}
ptr = HostGetStorageById(h, id3); ptr = HostGetStorageById(h, id3);
if (ptr != NULL) { FAIL_IF_NOT_NULL(ptr);
goto error;
}
void *ptr1a = HostAllocStorageById(h, id1); void *ptr1a = HostAllocStorageById(h, id1);
if (ptr1a == NULL) { FAIL_IF_NULL(ptr1a);
goto error;
}
void *ptr2a = HostAllocStorageById(h, id2); void *ptr2a = HostAllocStorageById(h, id2);
if (ptr2a == NULL) { FAIL_IF_NULL(ptr2a);
goto error;
}
void *ptr3a = HostAllocStorageById(h, id3); void *ptr3a = HostAllocStorageById(h, id3);
if (ptr3a == NULL) { FAIL_IF_NULL(ptr3a);
goto error;
}
void *ptr1b = HostGetStorageById(h, id1); void *ptr1b = HostGetStorageById(h, id1);
if (ptr1a != ptr1b) { FAIL_IF(ptr1a != ptr1b);
goto error;
}
void *ptr2b = HostGetStorageById(h, id2); void *ptr2b = HostGetStorageById(h, id2);
if (ptr2a != ptr2b) { FAIL_IF(ptr2a != ptr2b);
goto error;
}
void *ptr3b = HostGetStorageById(h, id3); void *ptr3b = HostGetStorageById(h, id3);
if (ptr3a != ptr3b) { FAIL_IF(ptr3a != ptr3b);
goto error;
}
HostRelease(h); HostRelease(h);
HostShutdown(); HostShutdown();
StorageCleanup(); StorageCleanup();
return 1; PASS;
error:
HostShutdown();
StorageCleanup();
return 0;
} }
static int HostStorageTest02(void) static int HostStorageTest02(void)
@ -208,11 +179,9 @@ static int HostStorageTest02(void)
StorageInit(); StorageInit();
HostStorageId id1 = HostStorageRegister("test", sizeof(void *), NULL, StorageTestFree); HostStorageId id1 = HostStorageRegister("test", sizeof(void *), NULL, StorageTestFree);
if (id1.id < 0) FAIL_IF(id1.id < 0);
goto error;
if (StorageFinalize() < 0) FAIL_IF(StorageFinalize() < 0);
goto error;
HostInitConfig(1); HostInitConfig(1);
@ -221,36 +190,23 @@ static int HostStorageTest02(void)
a.addr_data32[0] = 0x01020304; a.addr_data32[0] = 0x01020304;
a.family = AF_INET; a.family = AF_INET;
Host *h = HostGetHostFromHash(&a); Host *h = HostGetHostFromHash(&a);
if (h == NULL) { FAIL_IF_NULL(h);
printf("failed to get host: ");
goto error;
}
void *ptr = HostGetStorageById(h, id1); void *ptr = HostGetStorageById(h, id1);
if (ptr != NULL) { FAIL_IF_NOT_NULL(ptr);
goto error;
}
void *ptr1a = SCMalloc(128); void *ptr1a = SCMalloc(128);
if (unlikely(ptr1a == NULL)) { FAIL_IF_NULL(ptr1a);
goto error;
}
HostSetStorageById(h, id1, ptr1a); HostSetStorageById(h, id1, ptr1a);
void *ptr1b = HostGetStorageById(h, id1); void *ptr1b = HostGetStorageById(h, id1);
if (ptr1a != ptr1b) { FAIL_IF(ptr1a != ptr1b);
goto error;
}
HostRelease(h); HostRelease(h);
HostShutdown(); HostShutdown();
StorageCleanup(); StorageCleanup();
return 1; PASS;
error:
HostShutdown();
StorageCleanup();
return 0;
} }
static int HostStorageTest03(void) static int HostStorageTest03(void)
@ -259,17 +215,13 @@ static int HostStorageTest03(void)
StorageInit(); StorageInit();
HostStorageId id1 = HostStorageRegister("test1", sizeof(void *), NULL, StorageTestFree); HostStorageId id1 = HostStorageRegister("test1", sizeof(void *), NULL, StorageTestFree);
if (id1.id < 0) FAIL_IF(id1.id < 0);
goto error;
HostStorageId id2 = HostStorageRegister("test2", sizeof(void *), NULL, StorageTestFree); HostStorageId id2 = HostStorageRegister("test2", sizeof(void *), NULL, StorageTestFree);
if (id2.id < 0) FAIL_IF(id2.id < 0);
goto error;
HostStorageId id3 = HostStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree); HostStorageId id3 = HostStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree);
if (id3.id < 0) FAIL_IF(id3.id < 0);
goto error;
if (StorageFinalize() < 0) FAIL_IF(StorageFinalize() < 0);
goto error;
HostInitConfig(1); HostInitConfig(1);
@ -278,55 +230,34 @@ static int HostStorageTest03(void)
a.addr_data32[0] = 0x01020304; a.addr_data32[0] = 0x01020304;
a.family = AF_INET; a.family = AF_INET;
Host *h = HostGetHostFromHash(&a); Host *h = HostGetHostFromHash(&a);
if (h == NULL) { FAIL_IF_NULL(h);
printf("failed to get host: ");
goto error;
}
void *ptr = HostGetStorageById(h, id1); void *ptr = HostGetStorageById(h, id1);
if (ptr != NULL) { FAIL_IF_NOT_NULL(ptr);
goto error;
}
void *ptr1a = SCMalloc(128); void *ptr1a = SCMalloc(128);
if (unlikely(ptr1a == NULL)) { FAIL_IF_NULL(ptr1a);
goto error;
}
HostSetStorageById(h, id1, ptr1a); HostSetStorageById(h, id1, ptr1a);
void *ptr2a = SCMalloc(256); void *ptr2a = SCMalloc(256);
if (unlikely(ptr2a == NULL)) { FAIL_IF_NULL(ptr2a);
goto error;
}
HostSetStorageById(h, id2, ptr2a); HostSetStorageById(h, id2, ptr2a);
void *ptr3a = HostAllocStorageById(h, id3); void *ptr3a = HostAllocStorageById(h, id3);
if (ptr3a == NULL) { FAIL_IF_NULL(ptr3a);
goto error;
}
void *ptr1b = HostGetStorageById(h, id1); void *ptr1b = HostGetStorageById(h, id1);
if (ptr1a != ptr1b) { FAIL_IF(ptr1a != ptr1b);
goto error;
}
void *ptr2b = HostGetStorageById(h, id2); void *ptr2b = HostGetStorageById(h, id2);
if (ptr2a != ptr2b) { FAIL_IF(ptr2a != ptr2b);
goto error;
}
void *ptr3b = HostGetStorageById(h, id3); void *ptr3b = HostGetStorageById(h, id3);
if (ptr3a != ptr3b) { FAIL_IF(ptr3a != ptr3b);
goto error;
}
HostRelease(h); HostRelease(h);
HostShutdown(); HostShutdown();
StorageCleanup(); StorageCleanup();
return 1; PASS;
error:
HostShutdown();
StorageCleanup();
return 0;
} }
#endif #endif

Loading…
Cancel
Save