TEST_C(DequeTests, DequeFilterMut1) { int a = 1; int b = 2; int c = 3; int d = 4; int e = 5; int f = 6; deque_add_last(deque, &a); deque_add_last(deque, &b); deque_add_last(deque, &c); deque_add_last(deque, &d); deque_add_last(deque, &e); deque_add_last(deque, &f); CHECK_EQUAL_C_INT(6, deque_size(deque)); deque_filter_mut(deque, pred1); CHECK_EQUAL_C_INT(3, deque_size(deque)); int *removed = NULL; deque_remove_first(deque, (void*) &removed); CHECK_EQUAL_C_INT(a, *removed); deque_remove_first(deque, (void*) &removed); CHECK_EQUAL_C_INT(b, *removed); deque_remove_first(deque, (void*) &removed); CHECK_EQUAL_C_INT(c, *removed); };
TEST_C(DequeTests, DequeZipIterRemove) { deque_add(deque, "a"); deque_add(deque, "b"); deque_add(deque, "c"); deque_add(deque, "d"); Deque *d2; deque_new(&d2); deque_add(d2, "e"); deque_add(d2, "f"); deque_add(d2, "g"); DequeZipIter zip; deque_zip_iter_init(&zip, deque, d2); void *e1, *e2; void *r1, *r2; while (deque_zip_iter_next(&zip, &e1, &e2) != CC_ITER_END) { if (strcmp((char*) e1, "b") == 0) deque_zip_iter_remove(&zip, &r1, &r2); } CHECK_EQUAL_C_STRING("b", (char*)r1); CHECK_EQUAL_C_STRING("f", (char*)r2); CHECK_EQUAL_C_INT(0, deque_contains(deque, "b")); CHECK_EQUAL_C_INT(0, deque_contains(deque, "f")); CHECK_EQUAL_C_INT(3, deque_size(deque)); CHECK_EQUAL_C_INT(2, deque_size(d2)); deque_destroy(d2); };
TEST_C(DequeTests, DequeFilter3) { int a = 1; int b = 2; int c = 3; int d = 4; int e = 5; int f = 6; deque_add_last(deque, &a); deque_add_last(deque, &b); deque_add_last(deque, &c); deque_add_last(deque, &d); deque_add_last(deque, &e); deque_add_last(deque, &f); CHECK_EQUAL_C_INT(6, deque_size(deque)); Deque *filter = NULL; deque_filter(deque, pred3, &filter); const void * const* buff = deque_get_buffer(filter); CHECK_EQUAL_C_INT(1, deque_size(filter)); CHECK_EQUAL_C_POINTER(buff[0], &f); free(filter); };
TEST_C(DequeTests, DequeCopyDeep) { int *a = malloc(sizeof(int)); int *b = malloc(sizeof(int)); int *c = malloc(sizeof(int)); *a = 1; *b = 2; *c = 3; deque_add_last(deque, a); deque_add_last(deque, b); deque_add_last(deque, c); Deque *copy; deque_copy_deep(deque, cpy, ©); int size = deque_size(copy); CHECK_EQUAL_C_INT(3, size); int *ca; deque_get_at(copy, 0, (void*)&ca); int *cb; deque_get_at(copy, 1, (void*)&cb); int *cc; deque_get_at(copy, 2, (void*)&cc); CHECK_EQUAL_C_INT(1, *ca); CHECK_EQUAL_C_INT(2, *cb); CHECK_EQUAL_C_INT(3, *cc); deque_destroy_cb(copy, free); free(a); free(b); free(c); }
/* G:4 - Yield, network disconnected, disconnected manually */ TEST_C(YieldTests, YieldNetworkDisconnectedDisconnectedManually) { IoT_Error_t rc = aws_iot_mqtt_disconnect(&iotClient); CHECK_EQUAL_C_INT(SUCCESS, rc); rc = aws_iot_mqtt_yield(&iotClient, 1000); CHECK_EQUAL_C_INT(NETWORK_MANUALLY_DISCONNECTED, rc); }
TEST_C(DequeTests, DequeCopyShallow) { int a = 1; int b = 2; int c = 3; deque_add_last(deque, &a); deque_add_last(deque, &b); deque_add_last(deque, &c); Deque *copy; deque_copy_shallow(deque, ©); int size = deque_size(copy); CHECK_EQUAL_C_INT(3, size); int *ca; deque_get_at(copy, 0, (void*)&ca); int *cb; deque_get_at(copy, 1, (void*)&cb); int *cc; deque_get_at(copy, 2, (void*)&cc); CHECK_EQUAL_C_INT(a, *ca); CHECK_EQUAL_C_INT(b, *cb); CHECK_EQUAL_C_INT(c, *cc); deque_destroy(copy); };
/* G:8 - Yield, network connected, ping request/response */ TEST_C(YieldTests, PingRequestPingResponse) { IoT_Error_t rc = SUCCESS; int i = 0; int j = 0; int attempt = 3; IOT_DEBUG("-->Running Yield Tests - G:8 - Yield, network connected, ping request/response \n"); IOT_DEBUG("Current Keep Alive Interval is set to %d sec.\n", iotClient.clientData.keepAliveInterval); for(i = 0; i < attempt; i++) { IOT_DEBUG("[Round_%d/Total_%d] Waiting for %d sec...\n", i + 1, attempt, iotClient.clientData.keepAliveInterval); /* Set TLS buffer for ping response */ ResetTLSBuffer(); setTLSRxBufferForPingresp(); for(j = 0; j <= iotClient.clientData.keepAliveInterval; j++) { sleep(1); IOT_DEBUG("[Waited %d secs]", j + 1); rc = aws_iot_mqtt_yield(&iotClient, 100); CHECK_EQUAL_C_INT(SUCCESS, rc); } /* Check whether ping was processed correctly and new Ping request was generated */ CHECK_EQUAL_C_INT(1, isLastTLSTxMessagePingreq()); } IOT_DEBUG("-->Success - G:8 - Yield, network connected, ping request/response \n"); }
TEST_C(DequeTests, DequeAddAt5) { /* f == 0*/ int a = 1; int b = 2; int c = 3; int d = 4; int e = 5; int f = 6; int g = 7; deque_add_last(deque, &a); deque_add_last(deque, &b); deque_add_last(deque, &c); deque_add_last(deque, &d); deque_add_last(deque, &e); deque_add_last(deque, &f); deque_add_at(deque, &g, 1); const void * const* buff = deque_get_buffer(deque); const int elem = *((int*) buff[7]); CHECK_EQUAL_C_INT(elem, a); const int elem1 = *((int*) buff[0]); CHECK_EQUAL_C_INT(elem1, b); const int elem2 = *((int*) buff[5]); CHECK_EQUAL_C_INT(elem2, f); const int elem3 = *((int*) buff[1]); CHECK_EQUAL_C_INT(elem3, g); };
TEST_C(DequeTests, DequeAddAt4) { /* index < size / 2 && index_raw < last_raw*/ int a = 1; int b = 2; int c = 3; int d = 4; int e = 5; int f = 6; int g = 999; deque_add_last(deque, &a); deque_add_last(deque, &b); deque_add_last(deque, &c); deque_add_last(deque, &d); deque_add_last(deque, &e); deque_add_first(deque, &f); deque_add_at(deque, &g, 1); const void * const*buff = deque_get_buffer(deque); const int elem = *((int*) buff[0]); CHECK_EQUAL_C_INT(elem, g); const int elem1 = *((int*) buff[4]); CHECK_EQUAL_C_INT(elem1, e); const int elem2 = *((int*) buff[6]); CHECK_EQUAL_C_INT(elem2, f); const int elem3 = *((int*) buff[7]); CHECK_EQUAL_C_INT(elem3, a); };
/* G:6 - Yield, network disconnected, ping timeout, auto-reconnect disabled */ TEST_C(YieldTests, disconnectNoAutoReconnect) { IoT_Error_t rc = FAILURE; IOT_DEBUG("-->Running Yield Tests - G:6 - Yield, network disconnected, ping timeout, auto-reconnect disabled \n"); rc = aws_iot_mqtt_autoreconnect_set_status(&iotClient, true); CHECK_EQUAL_C_INT(SUCCESS, rc); CHECK_EQUAL_C_INT(true, aws_iot_mqtt_is_client_connected(&iotClient)); CHECK_EQUAL_C_INT(true, aws_iot_is_autoreconnect_enabled(&iotClient)); /* Disable Autoreconnect, then let ping request time out and call yield */ aws_iot_mqtt_autoreconnect_set_status(&iotClient, false); sleep((uint16_t)(iotClient.clientData.keepAliveInterval / 2)); ResetTLSBuffer(); /* Sleep for half keep alive interval to allow the first ping to be sent out */ sleep(iotClient.clientData.keepAliveInterval / (uint32_t)2); rc = aws_iot_mqtt_yield(&iotClient, 100); CHECK_EQUAL_C_INT(SUCCESS, rc); CHECK_EQUAL_C_INT(true, isLastTLSTxMessagePingreq()); /* Let ping request time out and call yield */ sleep(iotClient.clientData.keepAliveInterval / (uint32_t)2 + 1); rc = aws_iot_mqtt_yield(&iotClient, 100); CHECK_EQUAL_C_INT(NETWORK_DISCONNECTED_ERROR, rc); CHECK_EQUAL_C_INT(1, isLastTLSTxMessageDisconnect()); CHECK_EQUAL_C_INT(0, aws_iot_mqtt_is_client_connected(&iotClient)); CHECK_EQUAL_C_INT(true, dcHandlerInvoked); IOT_DEBUG("-->Success - G:6 - Yield, network disconnected, ping timeout, auto-reconnect disabled \n"); }
TEST_C(YieldTests, YieldNetworkDisconnectedNeverConnected) { AWS_IoT_Client tempIotClient; IoT_Error_t rc; InitMQTTParamsSetup(&initParams, AWS_IOT_MQTT_HOST, AWS_IOT_MQTT_PORT, false, iot_tests_unit_disconnect_handler); rc = aws_iot_mqtt_init(&tempIotClient, &initParams); CHECK_EQUAL_C_INT(SUCCESS, rc); rc = aws_iot_mqtt_yield(&tempIotClient, 1000); CHECK_EQUAL_C_INT(NETWORK_DISCONNECTED_ERROR, rc); }
/* G:9 - Yield, disconnected, Auto-reconnect timed-out */ TEST_C(YieldTests, disconnectAutoReconnectTimeout) { IoT_Error_t rc = FAILURE; IOT_DEBUG("-->Running Yield Tests - G:9 - Yield, disconnected, Auto-reconnect timed-out \n"); rc = aws_iot_mqtt_autoreconnect_set_status(&iotClient, true); CHECK_EQUAL_C_INT(SUCCESS, rc); CHECK_EQUAL_C_INT(true, aws_iot_mqtt_is_client_connected(&iotClient)); CHECK_EQUAL_C_INT(true, aws_iot_is_autoreconnect_enabled(&iotClient)); ResetTLSBuffer(); /* Sleep for half keep alive interval to allow the first ping to be sent out */ sleep(iotClient.clientData.keepAliveInterval / (uint32_t)2); rc = aws_iot_mqtt_yield(&iotClient, 100); CHECK_EQUAL_C_INT(SUCCESS, rc); CHECK_EQUAL_C_INT(true, isLastTLSTxMessagePingreq()); /* Let ping request time out and call yield */ sleep(iotClient.clientData.keepAliveInterval / (uint32_t)2 + 1); rc = aws_iot_mqtt_yield(&iotClient, 100); CHECK_EQUAL_C_INT(NETWORK_ATTEMPTING_RECONNECT, rc); CHECK_EQUAL_C_INT(0, aws_iot_mqtt_is_client_connected(&iotClient)); CHECK_EQUAL_C_INT(true, dcHandlerInvoked); IOT_DEBUG("-->Success - G:9 - Yield, disconnected, Auto-reconnect timed-out \n"); }
TEST_C(DequeTests, DequeTrimCapacity) { int a = 1; int b = 2; int c = 3; deque_add(deque, &a); deque_add(deque, &b); deque_add(deque, &c); CHECK_EQUAL_C_INT(8, deque_capacity(deque)); deque_trim_capacity(deque); CHECK_EQUAL_C_INT(4, deque_capacity(deque)); };
TEST_C(DequeTests, DequeIteratorNext) { int a = 1; int b = 2; int c = 3; int d = 4; int e = 5; int f = 6; deque_add(deque, &a); deque_add(deque, &b); deque_add(deque, &c); deque_add(deque, &d); deque_add(deque, &e); deque_add(deque, &f); DequeIter iter; deque_iter_init(&iter, deque); size_t i = 0; void *el; while (deque_iter_next(&iter, &el) != CC_ITER_END) { void *k; deque_get_at(deque, i, &k); CHECK_EQUAL_C_POINTER(k, el); i++; CHECK_EQUAL_C_INT(i, iter.index); } };
TEST_C(DequeTests, DequeZipIterNext) { deque_add(deque, "a"); deque_add(deque, "b"); deque_add(deque, "c"); deque_add(deque, "d"); Deque *d2; deque_new(&d2); deque_add(d2, "e"); deque_add(d2, "f"); deque_add(d2, "g"); DequeZipIter zip; deque_zip_iter_init(&zip, deque, d2); size_t i = 0; void *e1, *e2; while (deque_zip_iter_next(&zip, &e1, &e2) != CC_ITER_END) { if (i == 0) { CHECK_EQUAL_C_STRING("a", (char*)e1); CHECK_EQUAL_C_STRING("e", (char*)e2); } if (i == 2) { CHECK_EQUAL_C_STRING("c", (char*)e1); CHECK_EQUAL_C_STRING("g", (char*)e2); } i++; } CHECK_EQUAL_C_INT(3, i); deque_destroy(d2); };
/* F:4 - Disconnect attempt, not connected */ TEST_C(DisconnectTests, disconnectNotConnected) { IoT_Error_t rc = SUCCESS; IOT_DEBUG("-->Running Disconnect Tests - F:4 - Disconnect attempt, not connected \n"); /* First make sure client is disconnected */ rc = aws_iot_mqtt_disconnect(&iotClient); /* Check client is disconnected */ CHECK_EQUAL_C_INT(false, aws_iot_mqtt_is_client_connected(&iotClient)); /* Now call disconnect again */ rc = aws_iot_mqtt_disconnect(&iotClient); CHECK_EQUAL_C_INT(NETWORK_DISCONNECTED_ERROR, rc); IOT_DEBUG("-->Success - F:4 - Disconnect attempt, not connected \n"); }
TEST(TestHarness_c, checkInt) { CHECK_EQUAL_C_INT(2, 2); fixture->setTestFunction(_failIntMethod); fixture->runAllTests(); fixture->assertPrintContains("expected <1 0x1>\n but was <2 0x2>"); fixture->assertPrintContains("arness_c"); CHECK(!hasDestructorOfTheDestructorCheckedBeenCalled) }
TEST(TestHarness_c, checkInt) { CHECK_EQUAL_C_INT(2, 2); fixture->setTestFunction(_failIntMethod); fixture->runAllTests(); fixture->assertPrintContains("expected <1>\n but was <2>"); fixture->assertPrintContains("arness_c"); // fixture->assertPrintContains("TestHarness_cTest.cpp");; //becuase of MS lowercasing the files }
TEST_C(DequeTests, DequeGetAt) { int a = 1; int b = 2; int c = 3; deque_add(deque, &a); deque_add(deque, &b); deque_add(deque, &c); void *e; deque_get_at(deque, 1, &e); void *n; int status = deque_get_at(deque, 42, &n); CHECK_EQUAL_C_INT(b, *(int*)e); CHECK_EQUAL_C_INT(CC_ERR_OUT_OF_RANGE, status); };
TEST_C(DequeTestsConf, DequeBufferExpansion) { int a = 1; int b = 2; int c = 3; int d = 4; int e = 5; int f = 6; deque_add_first(deque, &a); deque_add_last(deque, &b); deque_add_first(deque, &c); deque_add_last(deque, &d); size_t capacity = deque_capacity(deque); CHECK_EQUAL_C_INT(4, capacity); /* Current layout: _________________ | b | d | c | a | ----------------- L F */ /* This line should trigger the buffer expansion */ deque_add_first(deque, &e); capacity = deque_capacity(deque); CHECK_EQUAL_C_INT(8, capacity); /* The expansion should align the elements.*/ const void * const* buff = deque_get_buffer(deque); const int elem = *((int*) buff[0]); CHECK_EQUAL_C_INT(elem, c); const int elem1 = *((int*) buff[1]); CHECK_EQUAL_C_INT(elem1, a); const int elem2 = *((int*) buff[2]); CHECK_EQUAL_C_INT(elem2, b); const int elem3 = *((int*) buff[3]); CHECK_EQUAL_C_INT(elem3, d); const int elem4 = *((int*) buff[7]); CHECK_EQUAL_C_INT(elem4, e); deque_add_last(deque, &f); const int elem5 = *((int*) buff[4]); CHECK_EQUAL_C_INT(elem5, f); };
TEST_C(DequeTests, DequeIteratorAdd) { int a = 1; int b = 2; int c = 3; int d = 4; int e = 5; int f = 6; int g = 7; deque_add(deque, &a); deque_add(deque, &b); deque_add(deque, &c); deque_add(deque, &d); deque_add(deque, &e); deque_add(deque, &f); DequeIter iter; deque_iter_init(&iter, deque); size_t i = 0; int *el; CHECK_EQUAL_C_INT(6, deque_size(deque)); while (deque_iter_next(&iter, (void*) &el) != CC_ITER_END) { if (*el == d) deque_iter_add(&iter, &g); if (i >= 3) { CHECK_EQUAL_C_INT(i, deque_iter_index(&iter) - 1); } i++; } CHECK_EQUAL_C_INT(7, deque_size(deque)); void *ret; deque_get_at(deque, 4, &ret); CHECK_EQUAL_C_INT(g, *(int*)ret); };
TEST_C(DequeTests, DequeRemoveAll) { int a = 1; int b = 2; int c = 3; deque_add(deque, &a); deque_add(deque, &b); deque_add(deque, &c); deque_remove_all(deque); void *first; int stat1 = deque_get_first(deque, &first); void *last; int stat2 = deque_get_last(deque, &last); CHECK_EQUAL_C_INT(CC_ERR_OUT_OF_RANGE, stat1); CHECK_EQUAL_C_INT(CC_ERR_OUT_OF_RANGE, stat2); CHECK_EQUAL_C_INT(0, deque_size(deque)); };
TEST_C(DequeTests, DequeContains) { int a = 1; int b = 2; int c = 3; int d = 4; int e = 5; int f = 6; int g = 7; deque_add(deque, &a); deque_add(deque, &b); deque_add(deque, &c); deque_add(deque, &d); deque_add(deque, &e); deque_add(deque, &f); deque_add(deque, &a); CHECK_EQUAL_C_INT(2, deque_contains(deque, &a)); CHECK_EQUAL_C_INT(0, deque_contains(deque, &g)); CHECK_EQUAL_C_INT(1, deque_contains(deque, &e)); };
TEST_C(DequeTests, DequeCapacity) { DequeConf conf; deque_conf_init(&conf); conf.capacity = 2; Deque *deque; deque_new_conf(&conf, &deque); CHECK_EQUAL_C_INT(2, deque_capacity(deque)); int a = 1; int b = 2; int c = 3; deque_add(deque, &a); deque_add(deque, &b); deque_add(deque, &c); CHECK_EQUAL_C_INT(4, deque_capacity(deque)); }
TEST_C(DequeTests, DequeZipIterReplace) { deque_add(deque, "a"); deque_add(deque, "b"); deque_add(deque, "c"); deque_add(deque, "d"); Deque *d2; deque_new(&d2); deque_add(d2, "e"); deque_add(d2, "f"); deque_add(d2, "g"); char *h = "h"; char *i = "i"; DequeZipIter zip; deque_zip_iter_init(&zip, deque, d2); void *e1, *e2; void *r1, *r2; while (deque_zip_iter_next(&zip, &e1, &e2) != CC_ITER_END) { if (strcmp((char*) e1, "b") == 0) deque_zip_iter_replace(&zip, h, i, &r1, &r2); } size_t index; deque_index_of(deque, "h", &index); CHECK_EQUAL_C_INT(1, index); deque_index_of(deque, "i", &index); CHECK_EQUAL_C_INT(1, index); CHECK_EQUAL_C_INT(1, deque_contains(deque, "h")); CHECK_EQUAL_C_INT(1, deque_contains(d2, "i")); deque_destroy(d2); };
TEST_C(DequeTests, DequeRemoveLast) { int a = 1; int b = 2; int c = 3; int d = 4; deque_add_first(deque, &a); deque_add_first(deque, &b); deque_add_last(deque, &c); deque_add_last(deque, &d); int *last; deque_get_last(deque, (void*) &last); CHECK_EQUAL_C_INT(d, *last); int *removed; deque_remove_last(deque, (void*) &removed); CHECK_EQUAL_C_INT(d, *removed); deque_get_last(deque, (void*) &last); CHECK_EQUAL_C_INT(c, *last); };
TEST_C(DequeTests, DequeReverse) { int a = 1; int b = 2; int c = 3; deque_add(deque, &a); deque_add(deque, &b); deque_add(deque, &c); deque_reverse(deque); int *ra; deque_get_at(deque, 0, (void*)&ra); int *rb; deque_get_at(deque, 1, (void*)&rb); int *rc; deque_get_at(deque, 2, (void*)&rc); CHECK_EQUAL_C_INT(c, *ra); CHECK_EQUAL_C_INT(b, *rb); CHECK_EQUAL_C_INT(a, *rc); };
TEST_C(DequeTests, DequeGetLast) { int a = 1; int b = 2; int c = 3; deque_add_first(deque, &a); deque_add_last(deque, &b); deque_add_first(deque, &c); int *last; deque_get_last(deque, (void*) &last); CHECK_EQUAL_C_INT(b, *last); };
TEST_C(DequeTests, DequeSize) { int a = 1; int b = 2; int c = 3; int d = 4; deque_add(deque, &a); deque_add(deque, &b); deque_add(deque, &c); deque_add(deque, &d); size_t size = deque_size(deque); CHECK_EQUAL_C_INT(4, size); };
TEST_C(DequeTests, DequeIteratorRemove) { int a = 1; int b = 2; int c = 3; int d = 4; int e = 5; int f = 6; deque_add(deque, &a); deque_add(deque, &b); deque_add(deque, &c); deque_add(deque, &d); deque_add(deque, &e); deque_add(deque, &f); DequeIter iter; deque_iter_init(&iter, deque); size_t i = 0; void *el; while (deque_iter_next(&iter, &el) != CC_ITER_END) { if (i == 3) deque_iter_remove(&iter, NULL); if (i > 2) { CHECK_EQUAL_C_INT(5, deque_size(deque)); } else { CHECK_EQUAL_C_INT(6, deque_size(deque)); } if (i >= 3) { CHECK_EQUAL_C_INT(i-1, deque_iter_index(&iter)); } i++; } };