4 * Copyright (C) 2015 Chris Jaekl
9 #include "qdtestcase.h"
17 //This is run before EACH TEST
24 void test_qdSockInit(void)
30 TEST_ASSERT_EQUAL(0, sock.sd);
31 TEST_ASSERT_EQUAL(0, sock.toWrite);
32 TEST_ASSERT_EQUAL(FALSE, sock.closeRequested);
35 void test_qdSockWrite_socketBusy(void)
45 qdMock_resetWriteBuf(0);
47 ret = qdSockWrite(&sock, buf, 32);
49 TEST_ASSERT_EQUAL(0, ret);
50 TEST_ASSERT_EQUAL(32, sock.toWrite);
51 TEST_ASSERT_EQUAL(FALSE, sock.closeRequested);
52 TEST_ASSERT_EQUAL(0, memcmp(buf, sock.buf, 32));
54 TEST_ASSERT_EQUAL(0, qdMock_getNumBytesWritten());
57 void test_qdSockWrite_partWritten(void)
63 memcpy(buf, "abcdefghijklmnopqrstuvwxyz789012", 32);
67 qdMock_resetWriteBuf(17);
69 ret = qdSockWrite(&sock, buf, 32);
71 TEST_ASSERT_EQUAL(17, ret);
72 TEST_ASSERT_EQUAL(15, sock.toWrite);
73 TEST_ASSERT_EQUAL(FALSE, sock.closeRequested);
74 TEST_ASSERT_EQUAL(0, memcmp(buf + 17, sock.buf, 15));
76 TEST_ASSERT_EQUAL(17, qdMock_getNumBytesWritten());
77 TEST_ASSERT_EQUAL(0, memcmp(buf, qdMock_getBytesWritten(), 17));
79 qdMock_resetWriteBuf(17);
81 ret = qdSockFlush(&sock);
83 TEST_ASSERT_EQUAL(15, ret);
84 TEST_ASSERT_EQUAL(0, sock.toWrite);
85 TEST_ASSERT_EQUAL(FALSE, sock.closeRequested);
87 TEST_ASSERT_EQUAL(15, qdMock_getNumBytesWritten());
88 TEST_ASSERT_EQUAL(0, memcmp(buf + 17, qdMock_getBytesWritten(), 15));
92 void test_qdSockWrite_writeWithVariedBuffering(void)
97 const char * expected;
101 memcpy(buf, "abcdefghijklmnopqrstuvwxyz7890123", WWVB_SIZE);
103 // Constrain socket writes to chunkSize bytes at a time.
104 // Try various different chunk sizes, and confirm that the
105 // whole write eventually succeeds, after enough calls to
108 for (chunkSize = 1; chunkSize <= (WWVB_SIZE + 1); ++chunkSize) {
110 qdMock_resetWriteBuf(chunkSize);
112 expected = &(buf[0]);
113 ret = qdSockWrite(&sock, buf, WWVB_SIZE);
116 actual = qdMock_getBytesWritten();
118 TEST_ASSERT_EQUAL(*expected, *actual);
124 qdMock_resetWriteBuf(chunkSize);
125 ret = qdSockFlush(&sock);
131 // TODO: need a lot more tests here
135 UnityBegin("test_qdsocket.c");
137 // RUN_TEST calls runTest
138 RUN_TEST(test_qdSockInit);
139 RUN_TEST(test_qdSockWrite_socketBusy);
140 RUN_TEST(test_qdSockWrite_partWritten);
141 RUN_TEST(test_qdSockWrite_writeWithVariedBuffering);