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 * Authors: Nathan Binkert
29 */
30
31#include <gtest/gtest.h>
32
33#include <cstdio>
34#include <sstream>
35#include <string>
36
37#include "base/cprintf.hh"
38
39#define CPRINTF_TEST(...)                                \
40    do {                                                 \
41        std::stringstream ss;                            \
42        ccprintf(ss, __VA_ARGS__);                       \
43        int maxlen = ss.str().length() + 3;              \
44        char *buf = new char[maxlen];                    \
45        buf[maxlen - 1] = '\0';                          \
46        snprintf(buf, maxlen - 2, __VA_ARGS__);          \
47        EXPECT_EQ(ss.str(), std::string(buf));           \
48        delete [] buf;                                   \
49    } while (0)
50
51TEST(CPrintf, Misc)
52{
53    char foo[] = "foo";
54    CPRINTF_TEST("%s\n", foo);
55
56    CPRINTF_TEST("%d\n", 'A');
57    CPRINTF_TEST("%shits%%s + %smisses%%s\n", "test", "test");
58    CPRINTF_TEST("%%s%-10s %c he went home \'\"%d %#o %#llx %1.5f %1.2E\n",
59                 "hello", 'A', 1, 0xff, 0xfffffffffffffULL, 3.141592653589,
60                 1.1e10);
61
62    CPRINTF_TEST("another test\n");
63
64    CPRINTF_TEST("%-10s %c he home \'\"%d %#o %#llx %1.5f %1.2E\n",
65                 "hello", 'A', 1, 0xff, 0xfffffffffffffULL,
66                 3.14159265, 1.1e10);
67}
68
69TEST(CPrintf, FloatingPoint)
70{
71    double f = 314159.26535897932384;
72
73    CPRINTF_TEST("%1.8f\n", f);
74    CPRINTF_TEST("%2.8f\n", f);
75    CPRINTF_TEST("%3.8f\n", f);
76    CPRINTF_TEST("%4.8f\n", f);
77    CPRINTF_TEST("%5.8f\n", f);
78    CPRINTF_TEST("%6.8f\n", f);
79    CPRINTF_TEST("%12.8f\n", f);
80    CPRINTF_TEST("%1000.8f\n", f);
81    CPRINTF_TEST("%1.0f\n", f);
82    CPRINTF_TEST("%1.1f\n", f);
83    CPRINTF_TEST("%1.2f\n", f);
84    CPRINTF_TEST("%1.3f\n", f);
85    CPRINTF_TEST("%1.4f\n", f);
86    CPRINTF_TEST("%1.5f\n", f);
87    CPRINTF_TEST("%1.6f\n", f);
88    CPRINTF_TEST("%1.7f\n", f);
89    CPRINTF_TEST("%1.8f\n", f);
90    CPRINTF_TEST("%1.9f\n", f);
91    CPRINTF_TEST("%1.10f\n", f);
92    CPRINTF_TEST("%1.11f\n", f);
93    CPRINTF_TEST("%1.12f\n", f);
94    CPRINTF_TEST("%1.13f\n", f);
95    CPRINTF_TEST("%1.14f\n", f);
96    CPRINTF_TEST("%1.15f\n", f);
97    CPRINTF_TEST("%1.16f\n", f);
98    CPRINTF_TEST("%1.17f\n", f);
99    CPRINTF_TEST("%1.18f\n", f);
100
101    f = 0.00000026535897932384;
102    CPRINTF_TEST("%1.8f\n", f);
103    CPRINTF_TEST("%2.8f\n", f);
104    CPRINTF_TEST("%3.8f\n", f);
105    CPRINTF_TEST("%4.8f\n", f);
106    CPRINTF_TEST("%5.8f\n", f);
107    CPRINTF_TEST("%6.8f\n", f);
108    CPRINTF_TEST("%12.8f\n", f);
109    CPRINTF_TEST("%1.0f\n", f);
110    CPRINTF_TEST("%1.1f\n", f);
111    CPRINTF_TEST("%1.2f\n", f);
112    CPRINTF_TEST("%1.3f\n", f);
113    CPRINTF_TEST("%1.4f\n", f);
114    CPRINTF_TEST("%1.5f\n", f);
115    CPRINTF_TEST("%1.6f\n", f);
116    CPRINTF_TEST("%1.7f\n", f);
117    CPRINTF_TEST("%1.8f\n", f);
118    CPRINTF_TEST("%1.9f\n", f);
119    CPRINTF_TEST("%1.10f\n", f);
120    CPRINTF_TEST("%1.11f\n", f);
121    CPRINTF_TEST("%1.12f\n", f);
122    CPRINTF_TEST("%1.13f\n", f);
123    CPRINTF_TEST("%1.14f\n", f);
124    CPRINTF_TEST("%1.15f\n", f);
125    CPRINTF_TEST("%1.16f\n", f);
126    CPRINTF_TEST("%1.17f\n", f);
127    CPRINTF_TEST("%1.18f\n", f);
128
129    f = 0.00000026535897932384;
130    CPRINTF_TEST("%1.8e\n", f);
131    CPRINTF_TEST("%2.8e\n", f);
132    CPRINTF_TEST("%3.8e\n", f);
133    CPRINTF_TEST("%4.8e\n", f);
134    CPRINTF_TEST("%5.8e\n", f);
135    CPRINTF_TEST("%6.8e\n", f);
136    CPRINTF_TEST("%12.8e\n", f);
137    CPRINTF_TEST("%1.0e\n", f);
138    CPRINTF_TEST("%1.1e\n", f);
139    CPRINTF_TEST("%1.2e\n", f);
140    CPRINTF_TEST("%1.3e\n", f);
141    CPRINTF_TEST("%1.4e\n", f);
142    CPRINTF_TEST("%1.5e\n", f);
143    CPRINTF_TEST("%1.6e\n", f);
144    CPRINTF_TEST("%1.7e\n", f);
145    CPRINTF_TEST("%1.8e\n", f);
146    CPRINTF_TEST("%1.9e\n", f);
147    CPRINTF_TEST("%1.10e\n", f);
148    CPRINTF_TEST("%1.11e\n", f);
149    CPRINTF_TEST("%1.12e\n", f);
150    CPRINTF_TEST("%1.13e\n", f);
151    CPRINTF_TEST("%1.14e\n", f);
152    CPRINTF_TEST("%1.15e\n", f);
153    CPRINTF_TEST("%1.16e\n", f);
154    CPRINTF_TEST("%1.17e\n", f);
155    CPRINTF_TEST("%1.18e\n", f);
156}
157
158TEST(CPrintf, Types)
159{
160    std::stringstream ss;
161
162    std::string foo1 = "string test";
163    ccprintf(ss, "%s\n", foo1);
164    EXPECT_EQ(ss.str(), "string test\n");
165    ss.str("");
166
167    std::stringstream foo2;
168    foo2 << "stringstream test";
169    ccprintf(ss, "%s\n", foo2.str());
170    EXPECT_EQ(ss.str(), "stringstream test\n");
171    ss.str("");
172
173    CPRINTF_TEST("%c  %c\n", 'c', 65);
174}
175
176TEST(CPrintf, SpecialFormatting)
177{
178    CPRINTF_TEST("%08.4f\n", 99.99);
179    CPRINTF_TEST("%0*.*f\n", 8, 4, 99.99);
180    CPRINTF_TEST("%07.*f\n", 4, 1.234);
181    CPRINTF_TEST("%#0*x\n", 9, 123412);
182}
183