cprintf.test.cc revision 13465:dee578a46d87
12SN/A/*
212276Sanouk.vanlaer@arm.com * Copyright (c) 2002-2005 The Regents of The University of Michigan
38707Sandreas.hansson@arm.com * All rights reserved.
48707Sandreas.hansson@arm.com *
58707Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without
68707Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are
78707Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright
88707Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer;
98707Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright
108707Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the
118707Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution;
128707Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its
138707Sandreas.hansson@arm.com * contributors may be used to endorse or promote products derived from
141762SN/A * this software without specific prior written permission.
157897Shestness@cs.utexas.edu *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272SN/A *
282SN/A * Authors: Nathan Binkert
292SN/A */
302SN/A
312SN/A#include <gtest/gtest.h>
322SN/A
332SN/A#include <cstdio>
342SN/A#include <sstream>
352SN/A#include <string>
362SN/A
372SN/A#include "base/cprintf.hh"
382SN/A
392SN/A#define CPRINTF_TEST(...)                                \
402665Ssaidi@eecs.umich.edu    do {                                                 \
412665Ssaidi@eecs.umich.edu        std::stringstream ss;                            \
422665Ssaidi@eecs.umich.edu        ccprintf(ss, __VA_ARGS__);                       \
437897Shestness@cs.utexas.edu        int maxlen = ss.str().length() + 3;              \
442SN/A        char *buf = new char[maxlen];                    \
452SN/A        buf[maxlen - 1] = '\0';                          \
461717SN/A        snprintf(buf, maxlen - 2, __VA_ARGS__);          \
471717SN/A        EXPECT_EQ(ss.str(), std::string(buf));           \
482SN/A        delete [] buf;                                   \
492SN/A    } while (0)
502SN/A
519850Sandreas.hansson@arm.comTEST(CPrintf, Misc)
529850Sandreas.hansson@arm.com{
539850Sandreas.hansson@arm.com    char foo[] = "foo";
549850Sandreas.hansson@arm.com    CPRINTF_TEST("%s\n", foo);
559850Sandreas.hansson@arm.com
569850Sandreas.hansson@arm.com    CPRINTF_TEST("%d\n", 'A');
578745Sgblack@eecs.umich.edu    CPRINTF_TEST("%shits%%s + %smisses%%s\n", "test", "test");
584182Sgblack@eecs.umich.edu    CPRINTF_TEST("%%s%-10s %c he went home \'\"%d %#o %#llx %1.5f %1.2E\n",
595664Sgblack@eecs.umich.edu                 "hello", 'A', 1, 0xff, 0xfffffffffffffULL, 3.141592653589,
60707SN/A                 1.1e10);
6113892Sgabeblack@google.com
6256SN/A    CPRINTF_TEST("another test\n");
638779Sgblack@eecs.umich.edu
644776Sgblack@eecs.umich.edu    CPRINTF_TEST("%-10s %c he home \'\"%d %#o %#llx %1.5f %1.2E\n",
6510464SAndreas.Sandberg@ARM.com                 "hello", 'A', 1, 0xff, 0xfffffffffffffULL,
6612284Sjose.marinho@arm.com                 3.14159265, 1.1e10);
679814Sandreas.hansson@arm.com}
6810529Smorr@cs.wisc.edu
692SN/ATEST(CPrintf, FloatingPoint)
7010529Smorr@cs.wisc.edu{
718901Sandreas.hansson@arm.com    double f = 314159.26535897932384;
722315SN/A
732680Sktlim@umich.edu    CPRINTF_TEST("%1.8f\n", f);
742SN/A    CPRINTF_TEST("%2.8f\n", f);
7510529Smorr@cs.wisc.edu    CPRINTF_TEST("%3.8f\n", f);
7610529Smorr@cs.wisc.edu    CPRINTF_TEST("%4.8f\n", f);
7710529Smorr@cs.wisc.edu    CPRINTF_TEST("%5.8f\n", f);
7810529Smorr@cs.wisc.edu    CPRINTF_TEST("%6.8f\n", f);
7910529Smorr@cs.wisc.edu    CPRINTF_TEST("%12.8f\n", f);
8010529Smorr@cs.wisc.edu    CPRINTF_TEST("%1000.8f\n", f);
8110529Smorr@cs.wisc.edu    CPRINTF_TEST("%1.0f\n", f);
8210529Smorr@cs.wisc.edu    CPRINTF_TEST("%1.1f\n", f);
8310529Smorr@cs.wisc.edu    CPRINTF_TEST("%1.2f\n", f);
8410529Smorr@cs.wisc.edu    CPRINTF_TEST("%1.3f\n", f);
8510529Smorr@cs.wisc.edu    CPRINTF_TEST("%1.4f\n", f);
8610529Smorr@cs.wisc.edu    CPRINTF_TEST("%1.5f\n", f);
8710529Smorr@cs.wisc.edu    CPRINTF_TEST("%1.6f\n", f);
882356SN/A    CPRINTF_TEST("%1.7f\n", f);
892356SN/A    CPRINTF_TEST("%1.8f\n", f);
902356SN/A    CPRINTF_TEST("%1.9f\n", f);
916144Sksewell@umich.edu    CPRINTF_TEST("%1.10f\n", f);
922356SN/A    CPRINTF_TEST("%1.11f\n", f);
932356SN/A    CPRINTF_TEST("%1.12f\n", f);
946144Sksewell@umich.edu    CPRINTF_TEST("%1.13f\n", f);
952356SN/A    CPRINTF_TEST("%1.14f\n", f);
962356SN/A    CPRINTF_TEST("%1.15f\n", f);
976144Sksewell@umich.edu    CPRINTF_TEST("%1.16f\n", f);
982356SN/A    CPRINTF_TEST("%1.17f\n", f);
992356SN/A    CPRINTF_TEST("%1.18f\n", f);
1002356SN/A
1016144Sksewell@umich.edu    f = 0.00000026535897932384;
1026144Sksewell@umich.edu    CPRINTF_TEST("%1.8f\n", f);
1036144Sksewell@umich.edu    CPRINTF_TEST("%2.8f\n", f);
1046144Sksewell@umich.edu    CPRINTF_TEST("%3.8f\n", f);
1056144Sksewell@umich.edu    CPRINTF_TEST("%4.8f\n", f);
1065336Shines@cs.fsu.edu    CPRINTF_TEST("%5.8f\n", f);
1072356SN/A    CPRINTF_TEST("%6.8f\n", f);
1082356SN/A    CPRINTF_TEST("%12.8f\n", f);
10913892Sgabeblack@google.com    CPRINTF_TEST("%1.0f\n", f);
1102SN/A    CPRINTF_TEST("%1.1f\n", f);
1111634SN/A    CPRINTF_TEST("%1.2f\n", f);
1129157Sandreas.hansson@arm.com    CPRINTF_TEST("%1.3f\n", f);
11310662SAli.Saidi@ARM.com    CPRINTF_TEST("%1.4f\n", f);
11410662SAli.Saidi@ARM.com    CPRINTF_TEST("%1.5f\n", f);
1153814Ssaidi@eecs.umich.edu    CPRINTF_TEST("%1.6f\n", f);
11610662SAli.Saidi@ARM.com    CPRINTF_TEST("%1.7f\n", f);
1175712Shsul@eecs.umich.edu    CPRINTF_TEST("%1.8f\n", f);
1185712Shsul@eecs.umich.edu    CPRINTF_TEST("%1.9f\n", f);
1195715Shsul@eecs.umich.edu    CPRINTF_TEST("%1.10f\n", f);
1205712Shsul@eecs.umich.edu    CPRINTF_TEST("%1.11f\n", f);
1215712Shsul@eecs.umich.edu    CPRINTF_TEST("%1.12f\n", f);
1221634SN/A    CPRINTF_TEST("%1.13f\n", f);
12310190Sakash.bagdia@arm.com    CPRINTF_TEST("%1.14f\n", f);
12410190Sakash.bagdia@arm.com    CPRINTF_TEST("%1.15f\n", f);
12510190Sakash.bagdia@arm.com    CPRINTF_TEST("%1.16f\n", f);
12610190Sakash.bagdia@arm.com    CPRINTF_TEST("%1.17f\n", f);
12710190Sakash.bagdia@arm.com    CPRINTF_TEST("%1.18f\n", f);
12810190Sakash.bagdia@arm.com
12910190Sakash.bagdia@arm.com    f = 0.00000026535897932384;
1308832SAli.Saidi@ARM.com    CPRINTF_TEST("%1.8e\n", f);
1318832SAli.Saidi@ARM.com    CPRINTF_TEST("%2.8e\n", f);
1328832SAli.Saidi@ARM.com    CPRINTF_TEST("%3.8e\n", f);
1338832SAli.Saidi@ARM.com    CPRINTF_TEST("%4.8e\n", f);
1348832SAli.Saidi@ARM.com    CPRINTF_TEST("%5.8e\n", f);
1358832SAli.Saidi@ARM.com    CPRINTF_TEST("%6.8e\n", f);
1369332Sdam.sunwoo@arm.com    CPRINTF_TEST("%12.8e\n", f);
1379332Sdam.sunwoo@arm.com    CPRINTF_TEST("%1.0e\n", f);
1389332Sdam.sunwoo@arm.com    CPRINTF_TEST("%1.1e\n", f);
1399332Sdam.sunwoo@arm.com    CPRINTF_TEST("%1.2e\n", f);
1409332Sdam.sunwoo@arm.com    CPRINTF_TEST("%1.3e\n", f);
1419332Sdam.sunwoo@arm.com    CPRINTF_TEST("%1.4e\n", f);
1429332Sdam.sunwoo@arm.com    CPRINTF_TEST("%1.5e\n", f);
1439332Sdam.sunwoo@arm.com    CPRINTF_TEST("%1.6e\n", f);
1449332Sdam.sunwoo@arm.com    CPRINTF_TEST("%1.7e\n", f);
1459332Sdam.sunwoo@arm.com    CPRINTF_TEST("%1.8e\n", f);
1469332Sdam.sunwoo@arm.com    CPRINTF_TEST("%1.9e\n", f);
1479430SAndreas.Sandberg@ARM.com    CPRINTF_TEST("%1.10e\n", f);
1489430SAndreas.Sandberg@ARM.com    CPRINTF_TEST("%1.11e\n", f);
1499430SAndreas.Sandberg@ARM.com    CPRINTF_TEST("%1.12e\n", f);
1509814Sandreas.hansson@arm.com    CPRINTF_TEST("%1.13e\n", f);
1519814Sandreas.hansson@arm.com    CPRINTF_TEST("%1.14e\n", f);
1529814Sandreas.hansson@arm.com    CPRINTF_TEST("%1.15e\n", f);
1531634SN/A    CPRINTF_TEST("%1.16e\n", f);
1548850Sandreas.hansson@arm.com    CPRINTF_TEST("%1.17e\n", f);
1558850Sandreas.hansson@arm.com    CPRINTF_TEST("%1.18e\n", f);
1568850Sandreas.hansson@arm.com}
1578850Sandreas.hansson@arm.com
1588850Sandreas.hansson@arm.comTEST(CPrintf, Types)
1598850Sandreas.hansson@arm.com{
1608850Sandreas.hansson@arm.com    std::stringstream ss;
16114198Sgabeblack@google.com
1628850Sandreas.hansson@arm.com    std::string foo1 = "string test";
1638850Sandreas.hansson@arm.com    ccprintf(ss, "%s\n", foo1);
16414197Sgabeblack@google.com    EXPECT_EQ(ss.str(), "string test\n");
16514197Sgabeblack@google.com    ss.str("");
16614197Sgabeblack@google.com
16714197Sgabeblack@google.com    std::stringstream foo2;
16814197Sgabeblack@google.com    foo2 << "stringstream test";
16914198Sgabeblack@google.com    ccprintf(ss, "%s\n", foo2.str());
17014198Sgabeblack@google.com    EXPECT_EQ(ss.str(), "stringstream test\n");
17114198Sgabeblack@google.com    ss.str("");
17214197Sgabeblack@google.com
17314197Sgabeblack@google.com    CPRINTF_TEST("%c  %c\n", 'c', 65);
17414197Sgabeblack@google.com}
1758850Sandreas.hansson@arm.com
1768850Sandreas.hansson@arm.comTEST(CPrintf, SpecialFormatting)
1778850Sandreas.hansson@arm.com{
1788850Sandreas.hansson@arm.com    CPRINTF_TEST("%08.4f\n", 99.99);
1798850Sandreas.hansson@arm.com    CPRINTF_TEST("%0*.*f\n", 8, 4, 99.99);
18014198Sgabeblack@google.com    CPRINTF_TEST("%07.*f\n", 4, 1.234);
1818850Sandreas.hansson@arm.com    CPRINTF_TEST("%#0*x\n", 9, 123412);
1825712Shsul@eecs.umich.edu}
18310110Sandreas.hansson@arm.com