Merge ktlim@zizzer:/bk/newmem
[gem5.git] / src / unittest / cprintftest.cc
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 <iostream>
32 #include <list>
33 #include <string>
34 #include <sstream>
35
36 #include "base/cprintf.hh"
37
38 using namespace std;
39
40 int
41 main()
42 {
43 char foo[9];
44 cprintf("%s\n", foo);
45
46 cprintf("%shits%%s + %smisses%%s\n", "test", "test");
47 cprintf("%%s%-10s %c he went home \'\"%d %#o %#x %1.5f %1.2E\n",
48 "hello", 'A', 1, 0xff, 0xfffffffffffffULL, 3.141592653589, 1.1e10);
49
50 cout << cformat("%s %#x %s\n") << "hello" << 0 << "foo 0\n";
51 cerr << cformat("%s %#x\n") << "hello" << 1 << "foo 1\n";
52
53 cprintf("another test\n");
54
55 stringstream buffer;
56 ccprintf(buffer, "%-10s %c he home \'\"%d %#o %#x %1.5f %1.2E\n",
57 "hello", 'A', 1, 0xff, 0xfffffffffffffULL, 3.14159265, 1.1e10);
58
59 double f = 314159.26535897932384;
60
61 #define ctest(x, y) printf(x, y); cprintf(x, y); cprintf("\n");
62 ctest("%1.8f\n", f);
63 ctest("%2.8f\n", f);
64 ctest("%3.8f\n", f);
65 ctest("%4.8f\n", f);
66 ctest("%5.8f\n", f);
67 ctest("%6.8f\n", f);
68 ctest("%12.8f\n", f);
69 ctest("%1000.8f\n", f);
70 ctest("%1.0f\n", f);
71 ctest("%1.1f\n", f);
72 ctest("%1.2f\n", f);
73 ctest("%1.3f\n", f);
74 ctest("%1.4f\n", f);
75 ctest("%1.5f\n", f);
76 ctest("%1.6f\n", f);
77 ctest("%1.7f\n", f);
78 ctest("%1.8f\n", f);
79 ctest("%1.9f\n", f);
80 ctest("%1.10f\n", f);
81 ctest("%1.11f\n", f);
82 ctest("%1.12f\n", f);
83 ctest("%1.13f\n", f);
84 ctest("%1.14f\n", f);
85 ctest("%1.15f\n", f);
86 ctest("%1.16f\n", f);
87 ctest("%1.17f\n", f);
88 ctest("%1.18f\n", f);
89
90 cout << "foo\n";
91
92 f = 0.00000026535897932384;
93 ctest("%1.8f\n", f);
94 ctest("%2.8f\n", f);
95 ctest("%3.8f\n", f);
96 ctest("%4.8f\n", f);
97 ctest("%5.8f\n", f);
98 ctest("%6.8f\n", f);
99 ctest("%12.8f\n", f);
100 ctest("%1.0f\n", f);
101 ctest("%1.1f\n", f);
102 ctest("%1.2f\n", f);
103 ctest("%1.3f\n", f);
104 ctest("%1.4f\n", f);
105 ctest("%1.5f\n", f);
106 ctest("%1.6f\n", f);
107 ctest("%1.7f\n", f);
108 ctest("%1.8f\n", f);
109 ctest("%1.9f\n", f);
110 ctest("%1.10f\n", f);
111 ctest("%1.11f\n", f);
112 ctest("%1.12f\n", f);
113 ctest("%1.13f\n", f);
114 ctest("%1.14f\n", f);
115 ctest("%1.15f\n", f);
116 ctest("%1.16f\n", f);
117 ctest("%1.17f\n", f);
118 ctest("%1.18f\n", f);
119
120 f = 0.00000026535897932384;
121 ctest("%1.8e\n", f);
122 ctest("%2.8e\n", f);
123 ctest("%3.8e\n", f);
124 ctest("%4.8e\n", f);
125 ctest("%5.8e\n", f);
126 ctest("%6.8e\n", f);
127 ctest("%12.8e\n", f);
128 ctest("%1.0e\n", f);
129 ctest("%1.1e\n", f);
130 ctest("%1.2e\n", f);
131 ctest("%1.3e\n", f);
132 ctest("%1.4e\n", f);
133 ctest("%1.5e\n", f);
134 ctest("%1.6e\n", f);
135 ctest("%1.7e\n", f);
136 ctest("%1.8e\n", f);
137 ctest("%1.9e\n", f);
138 ctest("%1.10e\n", f);
139 ctest("%1.11e\n", f);
140 ctest("%1.12e\n", f);
141 ctest("%1.13e\n", f);
142 ctest("%1.14e\n", f);
143 ctest("%1.15e\n", f);
144 ctest("%1.16e\n", f);
145 ctest("%1.17e\n", f);
146 ctest("%1.18e\n", f);
147
148 cout << buffer.str();
149
150 cout.width(0);
151 cout.precision(1);
152 cout << f << "\n";
153
154 string foo1 = "string test";
155 cprintf("%s\n", foo1);
156
157 stringstream foo2;
158 foo2 << "stringstream test";
159 cprintf("%s\n", foo2);
160
161 cprintf("%c %c\n", 'c', 65);
162
163 cout << '9';
164 return 0;
165 }