1 /* -*- c++ -*- */ 2 /* 3 * Copyright (C) 2010 The Android Open Source Project 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in 13 * the documentation and/or other materials provided with the 14 * distribution. 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 19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 20 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 23 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include "../include/iostream" 31 #ifndef ANDROID_ASTL_IOSTREAM__ 32 #error "Wrong header included!!" 33 #endif 34 #include "common.h" 35 #include <limits> 36 #include <string> 37 38 namespace android { 39 40 class A { 41 public: 42 A() { 43 mPassed = std::cout.precision() == 6; 44 } 45 static A mInstance; 46 bool mPassed; 47 }; 48 A A::mInstance; 49 50 bool testStaticInit() { 51 EXPECT_TRUE(A::mInstance.mPassed); 52 return true; 53 } 54 55 bool testOstream() { 56 EXPECT_TRUE(std::cout.precision() == 6); 57 EXPECT_TRUE(std::cerr.precision() == 6); 58 59 std::cout.precision(20); 60 std::cerr.precision(20); 61 62 EXPECT_TRUE(std::cout.precision() == 20); 63 EXPECT_TRUE(std::cerr.precision() == 20); 64 // reset back to the default value. 65 std::cout.precision(6); 66 std::cerr.precision(6); 67 return true; 68 } 69 70 bool testCoutCerr() { 71 std::cout << "Hi from stdout\n"; 72 std::cerr << "Hi from stderr\n"; 73 return true; 74 } 75 76 bool testManip() { 77 std::cout << "line 1" << std::endl 78 << " a nul char |" << std::ends << std::flush << "| in line 2."; 79 return true; 80 } 81 82 bool testOutputFormat() { 83 using std::endl; 84 using std::cout; 85 using std::numeric_limits; 86 cout << endl << "Int: " << numeric_limits<int>::max() << endl; 87 cout << "Negative int: " << numeric_limits<int>::min() << endl; 88 cout << "Unsigned int: " << numeric_limits<unsigned int>::max() << endl; 89 cout << "Long: " << numeric_limits<long>::max() << endl; 90 cout << "Negative long: " << numeric_limits<long>::min() << endl; 91 cout << "Unsigned long int: " << numeric_limits<unsigned long>::max() << endl; 92 cout << "Long long: " << numeric_limits<long long>::max() << endl; 93 cout << "Negative long long: " << numeric_limits<long long>::min() << endl; 94 cout << "Unsigned long long: " << numeric_limits<unsigned long long>::max() << endl; 95 96 cout.precision(std::numeric_limits<double>::digits10); 97 cout << "Double: " << numeric_limits<double>::max() << endl; 98 cout << "Negative double: " << numeric_limits<double>::min() << endl; 99 cout << "Float: " << numeric_limits<float>::max() << endl; 100 cout << "Negative float: " << numeric_limits<float>::min() << endl; 101 102 cout << "Void *: " << static_cast<void*>(&std::cout) << endl; 103 cout << "NULL *: " << static_cast<void*>(0) << endl; 104 cout << "bool: " << true << " " << false << endl; 105 cout << "char: " << 'A' << endl; 106 cout << "string: " << std::string("hello world") << endl; 107 return true; 108 } 109 } // namespace android 110 111 int main(int argc, char **argv){ 112 FAIL_UNLESS(testStaticInit); 113 FAIL_UNLESS(testOstream); 114 FAIL_UNLESS(testCoutCerr); 115 FAIL_UNLESS(testManip); 116 FAIL_UNLESS(testOutputFormat); 117 return kPassed; 118 } 119