gem5  v20.1.0.0
cprintf.test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2005 The Regents of The University of Michigan
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <gtest/gtest.h>
30 
31 #include <cstdio>
32 #include <sstream>
33 #include <string>
34 
35 #include "base/cprintf.hh"
36 
37 #define CPRINTF_TEST(...) \
38  do { \
39  std::stringstream ss; \
40  ccprintf(ss, __VA_ARGS__); \
41  int maxlen = ss.str().length() + 3; \
42  char *buf = new char[maxlen]; \
43  buf[maxlen - 1] = '\0'; \
44  snprintf(buf, maxlen - 2, __VA_ARGS__); \
45  EXPECT_EQ(ss.str(), std::string(buf)); \
46  delete [] buf; \
47  } while (0)
48 
49 TEST(CPrintf, Misc)
50 {
51  char foo[] = "foo";
52  CPRINTF_TEST("%s\n", foo);
53 
54  CPRINTF_TEST("%d\n", 'A');
55  CPRINTF_TEST("%shits%%s + %smisses%%s\n", "test", "test");
56  CPRINTF_TEST("%%s%-10s %c he went home \'\"%d %#o %#llx %1.5f %1.2E\n",
57  "hello", 'A', 1, 0xff, 0xfffffffffffffULL, 3.141592653589,
58  1.1e10);
59 
60  CPRINTF_TEST("another test\n");
61 
62  CPRINTF_TEST("%-10s %c he home \'\"%d %#o %#llx %1.5f %1.2E\n",
63  "hello", 'A', 1, 0xff, 0xfffffffffffffULL,
64  3.14159265, 1.1e10);
65 }
66 
67 TEST(CPrintf, FloatingPoint)
68 {
69  double f = 314159.26535897932384;
70 
71  CPRINTF_TEST("%1.8f\n", f);
72  CPRINTF_TEST("%2.8f\n", f);
73  CPRINTF_TEST("%3.8f\n", f);
74  CPRINTF_TEST("%4.8f\n", f);
75  CPRINTF_TEST("%5.8f\n", f);
76  CPRINTF_TEST("%6.8f\n", f);
77  CPRINTF_TEST("%12.8f\n", f);
78  CPRINTF_TEST("%1000.8f\n", f);
79  CPRINTF_TEST("%1.0f\n", f);
80  CPRINTF_TEST("%1.1f\n", f);
81  CPRINTF_TEST("%1.2f\n", f);
82  CPRINTF_TEST("%1.3f\n", f);
83  CPRINTF_TEST("%1.4f\n", f);
84  CPRINTF_TEST("%1.5f\n", f);
85  CPRINTF_TEST("%1.6f\n", f);
86  CPRINTF_TEST("%1.7f\n", f);
87  CPRINTF_TEST("%1.8f\n", f);
88  CPRINTF_TEST("%1.9f\n", f);
89  CPRINTF_TEST("%1.10f\n", f);
90  CPRINTF_TEST("%1.11f\n", f);
91  CPRINTF_TEST("%1.12f\n", f);
92  CPRINTF_TEST("%1.13f\n", f);
93  CPRINTF_TEST("%1.14f\n", f);
94  CPRINTF_TEST("%1.15f\n", f);
95  CPRINTF_TEST("%1.16f\n", f);
96  CPRINTF_TEST("%1.17f\n", f);
97  CPRINTF_TEST("%1.18f\n", f);
98 
99  f = 0.00000026535897932384;
100  CPRINTF_TEST("%1.8f\n", f);
101  CPRINTF_TEST("%2.8f\n", f);
102  CPRINTF_TEST("%3.8f\n", f);
103  CPRINTF_TEST("%4.8f\n", f);
104  CPRINTF_TEST("%5.8f\n", f);
105  CPRINTF_TEST("%6.8f\n", f);
106  CPRINTF_TEST("%12.8f\n", f);
107  CPRINTF_TEST("%1.0f\n", f);
108  CPRINTF_TEST("%1.1f\n", f);
109  CPRINTF_TEST("%1.2f\n", f);
110  CPRINTF_TEST("%1.3f\n", f);
111  CPRINTF_TEST("%1.4f\n", f);
112  CPRINTF_TEST("%1.5f\n", f);
113  CPRINTF_TEST("%1.6f\n", f);
114  CPRINTF_TEST("%1.7f\n", f);
115  CPRINTF_TEST("%1.8f\n", f);
116  CPRINTF_TEST("%1.9f\n", f);
117  CPRINTF_TEST("%1.10f\n", f);
118  CPRINTF_TEST("%1.11f\n", f);
119  CPRINTF_TEST("%1.12f\n", f);
120  CPRINTF_TEST("%1.13f\n", f);
121  CPRINTF_TEST("%1.14f\n", f);
122  CPRINTF_TEST("%1.15f\n", f);
123  CPRINTF_TEST("%1.16f\n", f);
124  CPRINTF_TEST("%1.17f\n", f);
125  CPRINTF_TEST("%1.18f\n", f);
126 
127  f = 0.00000026535897932384;
128  CPRINTF_TEST("%1.8e\n", f);
129  CPRINTF_TEST("%2.8e\n", f);
130  CPRINTF_TEST("%3.8e\n", f);
131  CPRINTF_TEST("%4.8e\n", f);
132  CPRINTF_TEST("%5.8e\n", f);
133  CPRINTF_TEST("%6.8e\n", f);
134  CPRINTF_TEST("%12.8e\n", f);
135  CPRINTF_TEST("%1.0e\n", f);
136  CPRINTF_TEST("%1.1e\n", f);
137  CPRINTF_TEST("%1.2e\n", f);
138  CPRINTF_TEST("%1.3e\n", f);
139  CPRINTF_TEST("%1.4e\n", f);
140  CPRINTF_TEST("%1.5e\n", f);
141  CPRINTF_TEST("%1.6e\n", f);
142  CPRINTF_TEST("%1.7e\n", f);
143  CPRINTF_TEST("%1.8e\n", f);
144  CPRINTF_TEST("%1.9e\n", f);
145  CPRINTF_TEST("%1.10e\n", f);
146  CPRINTF_TEST("%1.11e\n", f);
147  CPRINTF_TEST("%1.12e\n", f);
148  CPRINTF_TEST("%1.13e\n", f);
149  CPRINTF_TEST("%1.14e\n", f);
150  CPRINTF_TEST("%1.15e\n", f);
151  CPRINTF_TEST("%1.16e\n", f);
152  CPRINTF_TEST("%1.17e\n", f);
153  CPRINTF_TEST("%1.18e\n", f);
154 }
155 
156 TEST(CPrintf, Types)
157 {
158  std::stringstream ss;
159 
160  std::string foo1 = "string test";
161  ccprintf(ss, "%s\n", foo1);
162  EXPECT_EQ(ss.str(), "string test\n");
163  ss.str("");
164 
165  std::stringstream foo2;
166  foo2 << "stringstream test";
167  ccprintf(ss, "%s\n", foo2.str());
168  EXPECT_EQ(ss.str(), "stringstream test\n");
169  ss.str("");
170 
171  CPRINTF_TEST("%c %c\n", 'c', 65);
172 }
173 
174 TEST(CPrintf, SpecialFormatting)
175 {
176  CPRINTF_TEST("%08.4f\n", 99.99);
177  CPRINTF_TEST("%0*.*f\n", 8, 4, 99.99);
178  CPRINTF_TEST("%07.*f\n", 4, 1.234);
179  CPRINTF_TEST("%#0*x\n", 9, 123412);
180 }
EXPECT_EQ
#define EXPECT_EQ(lhs, rhs)
A macro which verifies that lhs and rhs are equal to each other.
Definition: unittest.hh:110
ArmISA::ss
Bitfield< 21 > ss
Definition: miscregs_types.hh:56
TEST
TEST(CPrintf, Misc)
Definition: cprintf.test.cc:49
cprintf.hh
ccprintf
void ccprintf(cp::Print &print)
Definition: cprintf.hh:127
CPRINTF_TEST
#define CPRINTF_TEST(...)
Definition: cprintf.test.cc:37
GuestABI::foo
std::true_type foo(void(*)(ThreadContext *, const Ret &ret, State &state))
ArmISA::f
Bitfield< 6 > f
Definition: miscregs_types.hh:64

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17