113465Sgabeblack@google.com/*
213465Sgabeblack@google.com * Copyright (c) 2002-2005 The Regents of The University of Michigan
313465Sgabeblack@google.com * All rights reserved.
413465Sgabeblack@google.com *
513465Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
613465Sgabeblack@google.com * modification, are permitted provided that the following conditions are
713465Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
813465Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
913465Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1013465Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1113465Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1213465Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1313465Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1413465Sgabeblack@google.com * this software without specific prior written permission.
1513465Sgabeblack@google.com *
1613465Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1713465Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1813465Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1913465Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2013465Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2113465Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2213465Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2313465Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2413465Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2513465Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2613465Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2713465Sgabeblack@google.com *
2813465Sgabeblack@google.com * Authors: Nathan Binkert
2913465Sgabeblack@google.com */
3013465Sgabeblack@google.com
3113465Sgabeblack@google.com#include <gtest/gtest.h>
3213465Sgabeblack@google.com
3313465Sgabeblack@google.com#include <cstdio>
3413465Sgabeblack@google.com#include <sstream>
3513465Sgabeblack@google.com#include <string>
3613465Sgabeblack@google.com
3713465Sgabeblack@google.com#include "base/cprintf.hh"
3813465Sgabeblack@google.com
3913465Sgabeblack@google.com#define CPRINTF_TEST(...)                                \
4013465Sgabeblack@google.com    do {                                                 \
4113465Sgabeblack@google.com        std::stringstream ss;                            \
4213465Sgabeblack@google.com        ccprintf(ss, __VA_ARGS__);                       \
4313465Sgabeblack@google.com        int maxlen = ss.str().length() + 3;              \
4413465Sgabeblack@google.com        char *buf = new char[maxlen];                    \
4513465Sgabeblack@google.com        buf[maxlen - 1] = '\0';                          \
4613465Sgabeblack@google.com        snprintf(buf, maxlen - 2, __VA_ARGS__);          \
4713465Sgabeblack@google.com        EXPECT_EQ(ss.str(), std::string(buf));           \
4813465Sgabeblack@google.com        delete [] buf;                                   \
4913465Sgabeblack@google.com    } while (0)
5013465Sgabeblack@google.com
5113465Sgabeblack@google.comTEST(CPrintf, Misc)
5213465Sgabeblack@google.com{
5313465Sgabeblack@google.com    char foo[] = "foo";
5413465Sgabeblack@google.com    CPRINTF_TEST("%s\n", foo);
5513465Sgabeblack@google.com
5613465Sgabeblack@google.com    CPRINTF_TEST("%d\n", 'A');
5713465Sgabeblack@google.com    CPRINTF_TEST("%shits%%s + %smisses%%s\n", "test", "test");
5813465Sgabeblack@google.com    CPRINTF_TEST("%%s%-10s %c he went home \'\"%d %#o %#llx %1.5f %1.2E\n",
5913465Sgabeblack@google.com                 "hello", 'A', 1, 0xff, 0xfffffffffffffULL, 3.141592653589,
6013465Sgabeblack@google.com                 1.1e10);
6113465Sgabeblack@google.com
6213465Sgabeblack@google.com    CPRINTF_TEST("another test\n");
6313465Sgabeblack@google.com
6413465Sgabeblack@google.com    CPRINTF_TEST("%-10s %c he home \'\"%d %#o %#llx %1.5f %1.2E\n",
6513465Sgabeblack@google.com                 "hello", 'A', 1, 0xff, 0xfffffffffffffULL,
6613465Sgabeblack@google.com                 3.14159265, 1.1e10);
6713465Sgabeblack@google.com}
6813465Sgabeblack@google.com
6913465Sgabeblack@google.comTEST(CPrintf, FloatingPoint)
7013465Sgabeblack@google.com{
7113465Sgabeblack@google.com    double f = 314159.26535897932384;
7213465Sgabeblack@google.com
7313465Sgabeblack@google.com    CPRINTF_TEST("%1.8f\n", f);
7413465Sgabeblack@google.com    CPRINTF_TEST("%2.8f\n", f);
7513465Sgabeblack@google.com    CPRINTF_TEST("%3.8f\n", f);
7613465Sgabeblack@google.com    CPRINTF_TEST("%4.8f\n", f);
7713465Sgabeblack@google.com    CPRINTF_TEST("%5.8f\n", f);
7813465Sgabeblack@google.com    CPRINTF_TEST("%6.8f\n", f);
7913465Sgabeblack@google.com    CPRINTF_TEST("%12.8f\n", f);
8013465Sgabeblack@google.com    CPRINTF_TEST("%1000.8f\n", f);
8113465Sgabeblack@google.com    CPRINTF_TEST("%1.0f\n", f);
8213465Sgabeblack@google.com    CPRINTF_TEST("%1.1f\n", f);
8313465Sgabeblack@google.com    CPRINTF_TEST("%1.2f\n", f);
8413465Sgabeblack@google.com    CPRINTF_TEST("%1.3f\n", f);
8513465Sgabeblack@google.com    CPRINTF_TEST("%1.4f\n", f);
8613465Sgabeblack@google.com    CPRINTF_TEST("%1.5f\n", f);
8713465Sgabeblack@google.com    CPRINTF_TEST("%1.6f\n", f);
8813465Sgabeblack@google.com    CPRINTF_TEST("%1.7f\n", f);
8913465Sgabeblack@google.com    CPRINTF_TEST("%1.8f\n", f);
9013465Sgabeblack@google.com    CPRINTF_TEST("%1.9f\n", f);
9113465Sgabeblack@google.com    CPRINTF_TEST("%1.10f\n", f);
9213465Sgabeblack@google.com    CPRINTF_TEST("%1.11f\n", f);
9313465Sgabeblack@google.com    CPRINTF_TEST("%1.12f\n", f);
9413465Sgabeblack@google.com    CPRINTF_TEST("%1.13f\n", f);
9513465Sgabeblack@google.com    CPRINTF_TEST("%1.14f\n", f);
9613465Sgabeblack@google.com    CPRINTF_TEST("%1.15f\n", f);
9713465Sgabeblack@google.com    CPRINTF_TEST("%1.16f\n", f);
9813465Sgabeblack@google.com    CPRINTF_TEST("%1.17f\n", f);
9913465Sgabeblack@google.com    CPRINTF_TEST("%1.18f\n", f);
10013465Sgabeblack@google.com
10113465Sgabeblack@google.com    f = 0.00000026535897932384;
10213465Sgabeblack@google.com    CPRINTF_TEST("%1.8f\n", f);
10313465Sgabeblack@google.com    CPRINTF_TEST("%2.8f\n", f);
10413465Sgabeblack@google.com    CPRINTF_TEST("%3.8f\n", f);
10513465Sgabeblack@google.com    CPRINTF_TEST("%4.8f\n", f);
10613465Sgabeblack@google.com    CPRINTF_TEST("%5.8f\n", f);
10713465Sgabeblack@google.com    CPRINTF_TEST("%6.8f\n", f);
10813465Sgabeblack@google.com    CPRINTF_TEST("%12.8f\n", f);
10913465Sgabeblack@google.com    CPRINTF_TEST("%1.0f\n", f);
11013465Sgabeblack@google.com    CPRINTF_TEST("%1.1f\n", f);
11113465Sgabeblack@google.com    CPRINTF_TEST("%1.2f\n", f);
11213465Sgabeblack@google.com    CPRINTF_TEST("%1.3f\n", f);
11313465Sgabeblack@google.com    CPRINTF_TEST("%1.4f\n", f);
11413465Sgabeblack@google.com    CPRINTF_TEST("%1.5f\n", f);
11513465Sgabeblack@google.com    CPRINTF_TEST("%1.6f\n", f);
11613465Sgabeblack@google.com    CPRINTF_TEST("%1.7f\n", f);
11713465Sgabeblack@google.com    CPRINTF_TEST("%1.8f\n", f);
11813465Sgabeblack@google.com    CPRINTF_TEST("%1.9f\n", f);
11913465Sgabeblack@google.com    CPRINTF_TEST("%1.10f\n", f);
12013465Sgabeblack@google.com    CPRINTF_TEST("%1.11f\n", f);
12113465Sgabeblack@google.com    CPRINTF_TEST("%1.12f\n", f);
12213465Sgabeblack@google.com    CPRINTF_TEST("%1.13f\n", f);
12313465Sgabeblack@google.com    CPRINTF_TEST("%1.14f\n", f);
12413465Sgabeblack@google.com    CPRINTF_TEST("%1.15f\n", f);
12513465Sgabeblack@google.com    CPRINTF_TEST("%1.16f\n", f);
12613465Sgabeblack@google.com    CPRINTF_TEST("%1.17f\n", f);
12713465Sgabeblack@google.com    CPRINTF_TEST("%1.18f\n", f);
12813465Sgabeblack@google.com
12913465Sgabeblack@google.com    f = 0.00000026535897932384;
13013465Sgabeblack@google.com    CPRINTF_TEST("%1.8e\n", f);
13113465Sgabeblack@google.com    CPRINTF_TEST("%2.8e\n", f);
13213465Sgabeblack@google.com    CPRINTF_TEST("%3.8e\n", f);
13313465Sgabeblack@google.com    CPRINTF_TEST("%4.8e\n", f);
13413465Sgabeblack@google.com    CPRINTF_TEST("%5.8e\n", f);
13513465Sgabeblack@google.com    CPRINTF_TEST("%6.8e\n", f);
13613465Sgabeblack@google.com    CPRINTF_TEST("%12.8e\n", f);
13713465Sgabeblack@google.com    CPRINTF_TEST("%1.0e\n", f);
13813465Sgabeblack@google.com    CPRINTF_TEST("%1.1e\n", f);
13913465Sgabeblack@google.com    CPRINTF_TEST("%1.2e\n", f);
14013465Sgabeblack@google.com    CPRINTF_TEST("%1.3e\n", f);
14113465Sgabeblack@google.com    CPRINTF_TEST("%1.4e\n", f);
14213465Sgabeblack@google.com    CPRINTF_TEST("%1.5e\n", f);
14313465Sgabeblack@google.com    CPRINTF_TEST("%1.6e\n", f);
14413465Sgabeblack@google.com    CPRINTF_TEST("%1.7e\n", f);
14513465Sgabeblack@google.com    CPRINTF_TEST("%1.8e\n", f);
14613465Sgabeblack@google.com    CPRINTF_TEST("%1.9e\n", f);
14713465Sgabeblack@google.com    CPRINTF_TEST("%1.10e\n", f);
14813465Sgabeblack@google.com    CPRINTF_TEST("%1.11e\n", f);
14913465Sgabeblack@google.com    CPRINTF_TEST("%1.12e\n", f);
15013465Sgabeblack@google.com    CPRINTF_TEST("%1.13e\n", f);
15113465Sgabeblack@google.com    CPRINTF_TEST("%1.14e\n", f);
15213465Sgabeblack@google.com    CPRINTF_TEST("%1.15e\n", f);
15313465Sgabeblack@google.com    CPRINTF_TEST("%1.16e\n", f);
15413465Sgabeblack@google.com    CPRINTF_TEST("%1.17e\n", f);
15513465Sgabeblack@google.com    CPRINTF_TEST("%1.18e\n", f);
15613465Sgabeblack@google.com}
15713465Sgabeblack@google.com
15813465Sgabeblack@google.comTEST(CPrintf, Types)
15913465Sgabeblack@google.com{
16013465Sgabeblack@google.com    std::stringstream ss;
16113465Sgabeblack@google.com
16213465Sgabeblack@google.com    std::string foo1 = "string test";
16313465Sgabeblack@google.com    ccprintf(ss, "%s\n", foo1);
16413465Sgabeblack@google.com    EXPECT_EQ(ss.str(), "string test\n");
16513465Sgabeblack@google.com    ss.str("");
16613465Sgabeblack@google.com
16713465Sgabeblack@google.com    std::stringstream foo2;
16813465Sgabeblack@google.com    foo2 << "stringstream test";
16913465Sgabeblack@google.com    ccprintf(ss, "%s\n", foo2.str());
17013465Sgabeblack@google.com    EXPECT_EQ(ss.str(), "stringstream test\n");
17113465Sgabeblack@google.com    ss.str("");
17213465Sgabeblack@google.com
17313465Sgabeblack@google.com    CPRINTF_TEST("%c  %c\n", 'c', 65);
17413465Sgabeblack@google.com}
17513465Sgabeblack@google.com
17613465Sgabeblack@google.comTEST(CPrintf, SpecialFormatting)
17713465Sgabeblack@google.com{
17813465Sgabeblack@google.com    CPRINTF_TEST("%08.4f\n", 99.99);
17913465Sgabeblack@google.com    CPRINTF_TEST("%0*.*f\n", 8, 4, 99.99);
18013465Sgabeblack@google.com    CPRINTF_TEST("%07.*f\n", 4, 1.234);
18113465Sgabeblack@google.com    CPRINTF_TEST("%#0*x\n", 9, 123412);
18213465Sgabeblack@google.com}
183