tests/test_printf.cpp

Tue, 20 Jun 2023 19:46:01 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 20 Jun 2023 19:46:01 +0200
changeset 720
f73adc75e50a
parent 653
e081643aae2a
permissions
-rw-r--r--

add empty sections to features.md

636
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "cx/printf.h"
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 #include "cx/buffer.h"
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 #include <gtest/gtest.h>
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 #include "util_allocator.h"
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 class PrintfFixture : public ::testing::Test {
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 protected:
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 std::string buf;
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 CxTestingAllocator alloc;
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 void TearDown() override {
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 buf.clear();
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 ASSERT_TRUE(alloc.verify());
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 }
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 static size_t write_func(
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 void const *src,
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 size_t esize,
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 size_t ecount,
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 void *target
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 ) {
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 auto str = reinterpret_cast<char const *>(src);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 auto buf = reinterpret_cast<std::string *>(target);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 EXPECT_EQ(esize, 1);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 EXPECT_EQ(strlen(str), ecount);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 *buf = str;
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 return ecount;
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 }
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 };
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 TEST_F(PrintfFixture, BPrintf) {
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 CxBuffer buf;
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 cxBufferInit(&buf, nullptr, 64, &alloc, 0);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 auto r = cx_bprintf(&buf, "This %s aged %u years in a %2XSK.", "Test", 10, 0xca);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 EXPECT_EQ(r, 34);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 EXPECT_EQ(buf.size, 34);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 buf.space[r] = '\0';
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 EXPECT_STREQ(buf.space, "This Test aged 10 years in a CASK.");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 cxBufferDestroy(&buf);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 }
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 TEST_F(PrintfFixture, FPrintf) {
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 auto h = "Hello";
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 size_t r;
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 r = cx_fprintf(&buf, PrintfFixture::write_func, "teststring");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 EXPECT_EQ(r, 10);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 EXPECT_EQ(buf, "teststring");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 r = cx_fprintf(&buf, PrintfFixture::write_func, "[%10s]", h);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 EXPECT_EQ(r, 12);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 EXPECT_EQ(buf, "[ Hello]");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 r = cx_fprintf(&buf, PrintfFixture::write_func, "[%-10s]", h);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 EXPECT_EQ(r, 12);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 EXPECT_EQ(buf, "[Hello ]");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 r = cx_fprintf(&buf, PrintfFixture::write_func, "[%*s]", 10, h);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 EXPECT_EQ(r, 12);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 EXPECT_EQ(buf, "[ Hello]");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 r = cx_fprintf(&buf, PrintfFixture::write_func, "[%-10.*s]", 4, h);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 EXPECT_EQ(r, 12);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 EXPECT_EQ(buf, "[Hell ]");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 r = cx_fprintf(&buf, PrintfFixture::write_func, "[%-*.*s]", 10, 4, h);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 EXPECT_EQ(r, 12);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 EXPECT_EQ(buf, "[Hell ]");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 r = cx_fprintf(&buf, PrintfFixture::write_func, "%c", 'A');
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 EXPECT_EQ(r, 1);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 EXPECT_EQ(buf, "A");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 r = cx_fprintf(&buf, PrintfFixture::write_func, "%i %d %.6i %i %.0i %+i %i", 1, 2, 3, 0, 0, 4, -4);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 EXPECT_EQ(r, 19);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 EXPECT_EQ(buf, "1 2 000003 0 +4 -4");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 r = cx_fprintf(&buf, PrintfFixture::write_func, "%x %x %X %#x", 5, 10, 10, 6);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 EXPECT_EQ(r, 9);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 EXPECT_EQ(buf, "5 a A 0x6");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 r = cx_fprintf(&buf, PrintfFixture::write_func, "%o %#o %#o", 10, 10, 4);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 EXPECT_EQ(r, 9);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 EXPECT_EQ(buf, "12 012 04");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 r = cx_fprintf(&buf, PrintfFixture::write_func, "%05.2f %.2f %5.2f", 1.5, 1.5, 1.5);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 EXPECT_EQ(r, 16);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 EXPECT_EQ(buf, "01.50 1.50 1.50");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 r = cx_fprintf(&buf, PrintfFixture::write_func, "'%*c'", 5, 'x');
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 EXPECT_EQ(r, 7);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 EXPECT_EQ(buf, "' x'");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 r = cx_fprintf(&buf, PrintfFixture::write_func, "'%*c'", -5, 'x');
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 EXPECT_EQ(r, 7);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 EXPECT_EQ(buf, "'x '");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 }
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 TEST_F(PrintfFixture, BPrintfLargeString) {
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 CxBuffer buf;
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 cxBufferInit(&buf, nullptr, 64, &alloc, CX_BUFFER_AUTO_EXTEND);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 auto aaa = std::string(512, 'a');
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 auto bbb = std::string(512, 'b');
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 auto r = cx_bprintf(&buf, "After %s comes %s.", aaa.data(), bbb.data());
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 EXPECT_EQ(r, 1038);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 EXPECT_EQ(buf.size, 1038);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 cxBufferPut(&buf, 0);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 EXPECT_EQ(buf.space, std::string("After ") + aaa + " comes " + bbb + ".");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 cxBufferDestroy(&buf);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 }
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 TEST_F(PrintfFixture, BPrintfNoCap) {
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 CxBuffer buf;
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 char space[20];
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 memset(space, 'a', 20);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 cxBufferInit(&buf, space, 16, &alloc, 0);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 auto r = cx_bprintf(&buf, "Hello %s with more than %d chars.", "string", 16);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 EXPECT_EQ(r, 16);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 EXPECT_EQ(buf.size, 16);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 EXPECT_EQ(0, memcmp(space, "Hello string witaaaa", 20));
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 cxBufferDestroy(&buf);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 }
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 TEST_F(PrintfFixture, SPrintf) {
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 auto h = "Hello";
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164 std::vector<char *> fl;
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 cxmutstr r;
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167 r = cx_asprintf_a(&alloc, "teststring");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168 EXPECT_EQ(r.length, 10);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 EXPECT_STREQ(r.ptr, "teststring");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170 fl.push_back(r.ptr);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 r = cx_asprintf_a(&alloc, "[%10s]", h);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 EXPECT_EQ(r.length, 12);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 EXPECT_STREQ(r.ptr, "[ Hello]");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 fl.push_back(r.ptr);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 r = cx_asprintf_a(&alloc, "[%-10s]", h);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178 EXPECT_EQ(r.length, 12);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 EXPECT_STREQ(r.ptr, "[Hello ]");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 fl.push_back(r.ptr);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 r = cx_asprintf_a(&alloc, "[%*s]", 10, h);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 EXPECT_EQ(r.length, 12);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 EXPECT_STREQ(r.ptr, "[ Hello]");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 fl.push_back(r.ptr);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 r = cx_asprintf_a(&alloc, "[%-10.*s]", 4, h);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 EXPECT_EQ(r.length, 12);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 EXPECT_STREQ(r.ptr, "[Hell ]");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 fl.push_back(r.ptr);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 r = cx_asprintf_a(&alloc, "[%-*.*s]", 10, 4, h);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 EXPECT_EQ(r.length, 12);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 EXPECT_STREQ(r.ptr, "[Hell ]");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 fl.push_back(r.ptr);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 r = cx_asprintf_a(&alloc, "%c", 'A');
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 EXPECT_EQ(r.length, 1);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 EXPECT_STREQ(r.ptr, "A");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200 fl.push_back(r.ptr);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202 r = cx_asprintf_a(&alloc, "%i %d %.6i %i %.0i %+i %i", 1, 2, 3, 0, 0, 4, -4);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 EXPECT_EQ(r.length, 19);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204 EXPECT_STREQ(r.ptr, "1 2 000003 0 +4 -4");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205 fl.push_back(r.ptr);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 r = cx_asprintf_a(&alloc, "%x %x %X %#x", 5, 10, 10, 6);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208 EXPECT_EQ(r.length, 9);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
209 EXPECT_STREQ(r.ptr, "5 a A 0x6");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
210 fl.push_back(r.ptr);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
211
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
212 r = cx_asprintf_a(&alloc, "%o %#o %#o", 10, 10, 4);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
213 EXPECT_EQ(r.length, 9);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
214 EXPECT_STREQ(r.ptr, "12 012 04");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
215 fl.push_back(r.ptr);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
217 r = cx_asprintf_a(&alloc, "%05.2f %.2f %5.2f", 1.5, 1.5, 1.5);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
218 EXPECT_EQ(r.length, 16);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219 EXPECT_STREQ(r.ptr, "01.50 1.50 1.50");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
220 fl.push_back(r.ptr);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222 r = cx_asprintf_a(&alloc, "'%*c'", 5, 'x');
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223 EXPECT_EQ(r.length, 7);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
224 EXPECT_STREQ(r.ptr, "' x'");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
225 fl.push_back(r.ptr);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
226
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
227 r = cx_asprintf_a(&alloc, "'%*c'", -5, 'x');
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228 EXPECT_EQ(r.length, 7);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
229 EXPECT_STREQ(r.ptr, "'x '");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
230 fl.push_back(r.ptr);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
231
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
232 for (auto c: fl) {
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
233 auto s = cx_mutstrn(c, 0);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
234 cx_strfree_a(&alloc, &s);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
235 }
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
236 }
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
237
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
238 TEST_F(PrintfFixture, SPrintfLargeString) {
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
239 auto aaa = std::string(512, 'a');
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
240 auto bbb = std::string(512, 'b');
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
241
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
242 auto r = cx_asprintf_a(&alloc, "After %s comes %s.", aaa.data(), bbb.data());
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
243 EXPECT_EQ(r.length, 1038);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
244 EXPECT_EQ(r.ptr, std::string("After ") + aaa + " comes " + bbb + ".");
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
245 EXPECT_EQ(r.ptr[1038], '\0');
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
246
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
247 cx_strfree_a(&alloc, &r);
cfcc8cf0168c add printf tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
248 }

mercurial