src/server/test/writer.c

changeset 633
392ec9026b07
parent 415
d938228c382e
equal deleted inserted replaced
632:1defab20b477 633:392ec9026b07
34 #include <cx/buffer.h> 34 #include <cx/buffer.h>
35 35
36 #include "writer.h" 36 #include "writer.h"
37 #include "testutils.h" 37 #include "testutils.h"
38 38
39 UCX_TEST(test_writer_putc) { 39 CX_TEST(test_writer_putc) {
40 Session *sn = testutil_session(); 40 Session *sn = testutil_session();
41 TestIOStream *st = testutil_iostream(2048, TRUE); 41 TestIOStream *st = testutil_iostream(2048, TRUE);
42 CxBuffer *buf = st->buf; 42 CxBuffer *buf = st->buf;
43 43
44 UCX_TEST_BEGIN; 44 CX_TEST_DO {
45 45
46 Writer writer; 46 Writer writer;
47 char wbuf[1024]; 47 char wbuf[1024];
48 writer_init(&writer, st, wbuf, 4); 48 writer_init(&writer, st, wbuf, 4);
49 Writer *out = &writer; 49 Writer *out = &writer;
50
51 writer_putc(out, 'a');
52 CX_TEST_ASSERT(wbuf[0] == 'a');
53 CX_TEST_ASSERT(writer.pos == 1);
54
55 writer_putc(out, 'b');
56 CX_TEST_ASSERT(wbuf[1] == 'b');
57 CX_TEST_ASSERT(writer.pos == 2);
58
59 writer_putc(out, 'c');
60 writer_putc(out, 'd');
61 CX_TEST_ASSERT(wbuf[2] == 'c');
62 CX_TEST_ASSERT(wbuf[3] == 'd');
63
64 writer_putc(out, 'f'); // should flush the buffer
65 CX_TEST_ASSERT(wbuf[0] == 'f');
66 CX_TEST_ASSERT(writer.pos == 1);
67 CX_TEST_ASSERT(buf->space[0] == 'a');
68 CX_TEST_ASSERT(buf->space[1] == 'b');
69 CX_TEST_ASSERT(buf->pos == 4);
50 70
51 writer_putc(out, 'a'); 71 }
52 UCX_TEST_ASSERT(wbuf[0] == 'a', "1: wrong char at pos 0");
53 UCX_TEST_ASSERT(writer.pos == 1, "1: wrong pos");
54
55 writer_putc(out, 'b');
56 UCX_TEST_ASSERT(wbuf[1] == 'b', "2: wrong char at pos 1");
57 UCX_TEST_ASSERT(writer.pos == 2, "2: wrong pos");
58
59 writer_putc(out, 'c');
60 writer_putc(out, 'd');
61 UCX_TEST_ASSERT(wbuf[2] == 'c', "3: wrong char at pos 2");
62 UCX_TEST_ASSERT(wbuf[3] == 'd', "4: wrong char at pos 3");
63
64 writer_putc(out, 'f'); // should flush the buffer
65 UCX_TEST_ASSERT(wbuf[0] == 'f', "5: wrong char at pos 0");
66 UCX_TEST_ASSERT(writer.pos == 1, "5: wrong pos");
67 UCX_TEST_ASSERT(buf->space[0] == 'a', "5: wrong char at UcxBuffer pos 0");
68 UCX_TEST_ASSERT(buf->space[1] == 'b', "5: wrong char at UcxBuffer pos 1");
69 UCX_TEST_ASSERT(buf->pos == 4, "5: wrong UcxBuffer pos");
70
71 UCX_TEST_END;
72 testutil_iostream_destroy(st); 72 testutil_iostream_destroy(st);
73 testutil_destroy_session(sn); 73 testutil_destroy_session(sn);
74 } 74 }
75 75
76 UCX_TEST(test_writer_flush) { 76 CX_TEST(test_writer_flush) {
77 Session *sn = testutil_session(); 77 Session *sn = testutil_session();
78 TestIOStream *st = testutil_iostream(2048, TRUE); 78 TestIOStream *st = testutil_iostream(2048, TRUE);
79 CxBuffer *buf = st->buf; 79 CxBuffer *buf = st->buf;
80 80
81 UCX_TEST_BEGIN; 81 CX_TEST_DO {
82 82
83 Writer writer; 83 Writer writer;
84 char wbuf[1024]; 84 char wbuf[1024];
85 writer_init(&writer, st, wbuf, 4); 85 writer_init(&writer, st, wbuf, 4);
86 Writer *out = &writer; 86 Writer *out = &writer;
87
88 writer_putc(out, 'a');
89 CX_TEST_ASSERT(wbuf[0] == 'a');
90 CX_TEST_ASSERT(writer.pos == 1);
91
92 writer_flush(out);
93 CX_TEST_ASSERT(writer.pos == 0);
94 CX_TEST_ASSERT(buf->space[0] == 'a');
95 CX_TEST_ASSERT(buf->pos == 1);
96
97 writer_putc(out, 'b');
98 CX_TEST_ASSERT(wbuf[0] == 'b');
99 CX_TEST_ASSERT(writer.pos == 1);
87 100
88 writer_putc(out, 'a'); 101 }
89 UCX_TEST_ASSERT(wbuf[0] == 'a', "1: wrong char at pos 0");
90 UCX_TEST_ASSERT(writer.pos == 1, "1: wrong pos");
91
92 writer_flush(out);
93 UCX_TEST_ASSERT(writer.pos == 0, "wrong pos after flush");
94 UCX_TEST_ASSERT(buf->space[0] == 'a', "wrong UcxBuffer content");
95 UCX_TEST_ASSERT(buf->pos == 1, "wrong UcxBuffer pos");
96
97 writer_putc(out, 'b');
98 UCX_TEST_ASSERT(wbuf[0] == 'b', "2: wrong char at pos 0");
99 UCX_TEST_ASSERT(writer.pos == 1, "2: wrong pos");
100
101 UCX_TEST_END;
102 testutil_iostream_destroy(st); 102 testutil_iostream_destroy(st);
103 testutil_destroy_session(sn); 103 testutil_destroy_session(sn);
104 } 104 }
105 105
106 UCX_TEST(test_writer_put) { 106 CX_TEST(test_writer_put) {
107 Session *sn = testutil_session(); 107 Session *sn = testutil_session();
108 TestIOStream *st = testutil_iostream(2048, TRUE); 108 TestIOStream *st = testutil_iostream(2048, TRUE);
109 CxBuffer *buf = st->buf; 109 CxBuffer *buf = st->buf;
110 110
111 UCX_TEST_BEGIN; 111 CX_TEST_DO {
112 112
113 Writer writer; 113 Writer writer;
114 char wbuf[1024]; 114 char wbuf[1024];
115 writer_init(&writer, st, wbuf, 8); 115 writer_init(&writer, st, wbuf, 8);
116 Writer *out = &writer; 116 Writer *out = &writer;
117
118 writer_put(out, "abcd", 4);
119 CX_TEST_ASSERT(!memcmp(wbuf, "abcd", 4));
120 CX_TEST_ASSERT(writer.pos == 4);
121
122 writer_put(out, "efgh", 4);
123 CX_TEST_ASSERT(!memcmp(wbuf, "abcdefgh", 8));
124 CX_TEST_ASSERT(writer.pos == 8);
125
126 writer_put(out, "1234", 4);
127 CX_TEST_ASSERT(!memcmp(wbuf, "1234", 4));
128 CX_TEST_ASSERT(writer.pos == 4);
129 CX_TEST_ASSERT(!memcmp(buf->space, "abcdefgh", 8));
130 CX_TEST_ASSERT(buf->pos == 8);
131
132 writer_put(out, "5678xx", 6);
133 CX_TEST_ASSERT(!memcmp(wbuf, "xx", 2));
134 CX_TEST_ASSERT(writer.pos == 2);
135 CX_TEST_ASSERT(!memcmp(buf->space, "abcdefgh12345678", 16));
136 CX_TEST_ASSERT(buf->pos == 16);
137
138 writer_puts(out, cx_str("345678abcdefgh12345678end."));
139 CX_TEST_ASSERT(!memcmp(wbuf, "end.", 4));
140 CX_TEST_ASSERT(writer.pos == 4);
141 CX_TEST_ASSERT(!memcmp(buf->space, "abcdefgh12345678xx345678abcdefgh12345678", 40));
142 CX_TEST_ASSERT(buf->pos == 40);
117 143
118 writer_put(out, "abcd", 4); 144 }
119 UCX_TEST_ASSERT(!memcmp(wbuf, "abcd", 4), "1: wrong content");
120 UCX_TEST_ASSERT(writer.pos == 4, "1: wrong pos");
121
122 writer_put(out, "efgh", 4);
123 UCX_TEST_ASSERT(!memcmp(wbuf, "abcdefgh", 8), "2: wrong content");
124 UCX_TEST_ASSERT(writer.pos == 8, "2: wrong pos");
125
126 writer_put(out, "1234", 4);
127 UCX_TEST_ASSERT(!memcmp(wbuf, "1234", 4), "3: wrong content");
128 UCX_TEST_ASSERT(writer.pos == 4, "3: wrong pos");
129 UCX_TEST_ASSERT(!memcmp(buf->space, "abcdefgh", 8), "3: wrong UcxBuffer content");
130 UCX_TEST_ASSERT(buf->pos == 8, "3: wrong UcxBuffer pos");
131
132 writer_put(out, "5678xx", 6);
133 UCX_TEST_ASSERT(!memcmp(wbuf, "xx", 2), "4: wrong content");
134 UCX_TEST_ASSERT(writer.pos == 2, "4: wrong pos");
135 UCX_TEST_ASSERT(!memcmp(buf->space, "abcdefgh12345678", 16), "4: wrong UcxBuffer content");
136 UCX_TEST_ASSERT(buf->pos == 16, "4: wrong UcxBuffer pos");
137
138 writer_puts(out, cx_str("345678abcdefgh12345678end."));
139 UCX_TEST_ASSERT(!memcmp(wbuf, "end.", 4), "5: wrong content");
140 UCX_TEST_ASSERT(writer.pos == 4, "5: wrong pos");
141 UCX_TEST_ASSERT(!memcmp(
142 buf->space,
143 "abcdefgh12345678xx345678abcdefgh12345678",
144 40),
145 "5: wrong UcxBuffer content");
146 UCX_TEST_ASSERT(buf->pos == 40, "5: wrong UcxBuffer pos");
147
148 UCX_TEST_END;
149 testutil_iostream_destroy(st); 145 testutil_iostream_destroy(st);
150 testutil_destroy_session(sn); 146 testutil_destroy_session(sn);
151 } 147 }

mercurial