Wed, 02 May 2018 20:55:23 +0200
minor formatting fix in modules.md
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
259 | 4 | * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. |
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | #include "utils_tests.h" |
251
fae240d633fc
changes source directory structure in preperation for autotools rollout
Mike Becker <universe@uap-core.de>
parents:
250
diff
changeset
|
30 | #include <ucx/buffer.h> |
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31 | |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32 | UCX_TEST(test_ucx_fprintf) { |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33 | UcxBuffer *b1 = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | UcxBuffer *b2 = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | char *teststr1 = (char*)calloc(1, 1024); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | char *teststr2 = (char*)calloc(1, 1024); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | memset(teststr1, 'a', 1023); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | memset(teststr2, 'b', 1023); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
40 | |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
41 | UCX_TEST_BEGIN |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
42 | |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
43 | ucx_fprintf(b1, (write_func)ucx_buffer_write, "Hello %s", "World"); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
44 | UCX_TEST_ASSERT(!strcmp(b1->space, "Hello World"), "wrong content in b1"); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
45 | ucx_fprintf(b1, (write_func)ucx_buffer_write, "\nend.\n"); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46 | UCX_TEST_ASSERT(!strcmp(b1->space, "Hello World\nend.\n"), |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
47 | "wrong content in b1 after second fprintf"); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48 | |
147
1aa598f36872
added printf for UcxBuffer + fixed memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
145
diff
changeset
|
49 | ucx_bprintf(b2, "%s%s", teststr1, teststr2); |
145
e974640ec4e0
fixed utils tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
50 | UCX_TEST_ASSERT(b2->pos == 2046, "wrong length"); |
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51 | UCX_TEST_ASSERT(!memcmp(b2->space, teststr1, 1023), |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52 | "wrong first half in b2"); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
53 | UCX_TEST_ASSERT(!memcmp(b2->space+1023, teststr2, 1023), |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
54 | "wrong second half in b2"); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
55 | |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
56 | UCX_TEST_END |
145
e974640ec4e0
fixed utils tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
57 | |
e974640ec4e0
fixed utils tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
58 | ucx_buffer_free(b1); |
e974640ec4e0
fixed utils tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
59 | ucx_buffer_free(b2); |
147
1aa598f36872
added printf for UcxBuffer + fixed memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
145
diff
changeset
|
60 | free(teststr1); |
1aa598f36872
added printf for UcxBuffer + fixed memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
145
diff
changeset
|
61 | free(teststr2); |
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
62 | } |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63 | |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
64 | UCX_TEST(test_ucx_asprintf) { |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65 | char *teststr1 = (char*)calloc(1, 1024); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
66 | char *teststr2 = (char*)calloc(1, 1024); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | memset(teststr1, 'a', 1023); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68 | memset(teststr2, 'b', 1023); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | UcxAllocator *a = ucx_default_allocator(); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70 | |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
71 | UCX_TEST_BEGIN |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
72 | |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
73 | sstr_t s1 = ucx_asprintf(a, "int: %d\nHello %s!", 123, "World"); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74 | UCX_TEST_ASSERT(s1.ptr, "s1.ptr is NULL"); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
75 | UCX_TEST_ASSERT(s1.length == 21, "wrong length"); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76 | UCX_TEST_ASSERT(!sstrcmp(s1, S("int: 123\nHello World!")), "wrong content"); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
77 | free(s1.ptr); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
78 | |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
79 | sstr_t s2 = ucx_asprintf(a, "%s%s", teststr1, teststr2); |
145
e974640ec4e0
fixed utils tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
80 | UCX_TEST_ASSERT(s2.ptr, "s2.ptr is NULL"); |
e974640ec4e0
fixed utils tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
81 | UCX_TEST_ASSERT(s2.length == 2046, "wrong length"); |
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
82 | UCX_TEST_ASSERT(!memcmp(s2.ptr, teststr1, 1023), |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
83 | "wrong first half in s2"); |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
84 | UCX_TEST_ASSERT(!memcmp(s2.ptr+1023, teststr2, 1023), |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
85 | "wrong second half in s2"); |
145
e974640ec4e0
fixed utils tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
86 | free(s2.ptr); |
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
87 | |
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
88 | UCX_TEST_END |
147
1aa598f36872
added printf for UcxBuffer + fixed memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
145
diff
changeset
|
89 | |
1aa598f36872
added printf for UcxBuffer + fixed memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
145
diff
changeset
|
90 | free(teststr1); |
1aa598f36872
added printf for UcxBuffer + fixed memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
145
diff
changeset
|
91 | free(teststr2); |
142
ee8cb27d8b8e
added printf functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
92 | } |
168 | 93 | |
223
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
94 | UCX_TEST(test_ucx_sprintf) { |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
95 | UCX_TEST_BEGIN |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
96 | |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
97 | sstr_t s1 = ucx_sprintf("int: %d\nHello %s!", 123, "World"); |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
98 | UCX_TEST_ASSERT(s1.ptr, "s1.ptr is NULL"); |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
99 | UCX_TEST_ASSERT(s1.length == 21, "wrong length"); |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
100 | UCX_TEST_ASSERT(!sstrcmp(s1, S("int: 123\nHello World!")), "wrong content"); |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
101 | free(s1.ptr); |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
102 | |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
103 | sstr_t s2 = ucx_sprintf("Nothing to format!"); |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
104 | UCX_TEST_ASSERT(s2.ptr, "s2.ptr is NULL"); |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
105 | UCX_TEST_ASSERT(s2.length == 18, "wrong length"); |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
106 | UCX_TEST_ASSERT(!memcmp(s2.ptr, "Nothing to format!", 18), |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
107 | "wrong string without format arguments"); |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
108 | free(s2.ptr); |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
109 | |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
110 | UCX_TEST_END |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
111 | } |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
112 | |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
113 | UCX_TEST(test_ucx_bprintf) { |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
114 | UcxBuffer *b = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT); |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
115 | |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
116 | UCX_TEST_BEGIN |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
117 | |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
118 | ucx_bprintf(b, "int: %d\nHello %s!", 123, "World"); |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
119 | UCX_TEST_ASSERT(b->size == 21, "wrong length"); |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
120 | UCX_TEST_ASSERT(!memcmp(b->space, "int: 123\nHello World!", 21), |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
121 | "wrong content"); |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
122 | |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
123 | ucx_buffer_clear(b); |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
124 | |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
125 | ucx_bprintf(b, "Nothing to format!"); |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
126 | UCX_TEST_ASSERT(b->size == 18, "wrong length"); |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
127 | UCX_TEST_ASSERT(!memcmp(b->space, "Nothing to format!", 18), |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
128 | "wrong string without format arguments"); |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
129 | |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
130 | UCX_TEST_END |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
131 | |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
132 | ucx_buffer_free(b); |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
133 | } |
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
134 | |
168 | 135 | UCX_TEST(test_ucx_stream_copy) { |
136 | UcxBuffer *b1 = ucx_buffer_new(NULL, 64, UCX_BUFFER_DEFAULT); | |
137 | UcxBuffer *b2 = ucx_buffer_new(NULL, 2, UCX_BUFFER_AUTOEXTEND); | |
138 | ||
139 | UCX_TEST_BEGIN | |
140 | ||
141 | ucx_buffer_write("01234567", 1, 8, b1); | |
142 | ucx_buffer_write("abcdefgh", 1, 8, b1); | |
143 | UCX_TEST_ASSERT(b1->size == 16, "failed to fill buffer b1"); | |
144 | ucx_buffer_seek(b1, 0, SEEK_SET); | |
145 | ||
222
e0f850709a5c
changes ucx_stream_Xcopy API
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
146 | char copybuf[256]; |
e0f850709a5c
changes ucx_stream_Xcopy API
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
147 | size_t ncp = ucx_stream_bcopy(b1, b2, ucx_buffer_read, ucx_buffer_write, |
e0f850709a5c
changes ucx_stream_Xcopy API
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
148 | copybuf, sizeof(copybuf)); |
168 | 149 | UCX_TEST_ASSERT(ncp == 16, "wrong number of copied bytes"); |
150 | UCX_TEST_ASSERT(b2->size == 16, "b2 has wrong size"); | |
151 | UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0, | |
152 | "b1 and b2 have not the same content"); | |
153 | ||
154 | memset(b2->space, 0, b2->capacity); | |
155 | b2->pos = 0; | |
156 | b2->size = 0; | |
157 | ucx_buffer_seek(b1, 0, SEEK_SET); | |
158 | ||
159 | FILE *file = tmpfile(); | |
160 | UCX_TEST_ASSERT(file, "test file cannot be opened, test aborted"); | |
161 | ||
222
e0f850709a5c
changes ucx_stream_Xcopy API
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
162 | ncp = ucx_stream_copy(b1, file, ucx_buffer_read, fwrite); |
168 | 163 | UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes to file"); |
164 | ||
165 | fseek(file, 0, SEEK_SET); | |
166 | ||
222
e0f850709a5c
changes ucx_stream_Xcopy API
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
167 | ncp = ucx_stream_copy(file, b2, fread, ucx_buffer_write); |
168 | 168 | UCX_TEST_ASSERT(ncp == 16, "copied wrong number of bytes from file"); |
169 | ||
170 | UCX_TEST_ASSERT(memcmp(b1->space, b2->space, 16) == 0, | |
171 | "b1 and b2 content mismatch"); | |
172 | ||
173 | fclose(file); | |
174 | ||
175 | ucx_buffer_clear(b1); | |
176 | ucx_buffer_seek(b2, 0, SEEK_SET); | |
177 | ncp = ucx_stream_ncopy(b2, b1, ucx_buffer_read, ucx_buffer_write, 8); | |
178 | UCX_TEST_ASSERT(ncp == 8, "copied wrong number of bytes with ncopy"); | |
179 | UCX_TEST_ASSERT(memcmp(b1->space, "01234567\0\0\0\0\0\0\0\0", 16) == 0, | |
180 | "content wrong after ncopy"); | |
181 | ||
182 | UCX_TEST_END | |
183 | ||
184 | ucx_buffer_free(b1); | |
185 | ucx_buffer_free(b2); | |
186 | } | |
223
e18884bbad48
fixes ucx_sprintf macro and adds tests for sprintf and bprintf
Mike Becker <universe@uap-core.de>
parents:
222
diff
changeset
|
187 |