OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string.h> |
| 6 #include <limits> |
| 7 |
| 8 #include "include/v8stdint.h" |
| 9 #include "src/ostreams.h" |
| 10 #include "test/cctest/cctest.h" |
| 11 |
| 12 using namespace v8::internal; |
| 13 |
| 14 |
| 15 TEST(OStringStreamConstructor) { |
| 16 OStringStream oss; |
| 17 CHECK_EQ(0, oss.size()); |
| 18 CHECK_GT(oss.capacity(), 0); |
| 19 CHECK_NE(NULL, oss.data()); |
| 20 CHECK_EQ("", oss.c_str()); |
| 21 } |
| 22 |
| 23 |
| 24 #define TEST_STRING \ |
| 25 "Ash nazg durbatuluk, " \ |
| 26 "ash nazg gimbatul, " \ |
| 27 "ash nazg thrakatuluk, " \ |
| 28 "agh burzum-ishi krimpatul." |
| 29 |
| 30 TEST(OStringStreamGrow) { |
| 31 OStringStream oss; |
| 32 const int repeat = 30; |
| 33 size_t len = strlen(TEST_STRING); |
| 34 for (int i = 0; i < repeat; ++i) { |
| 35 oss.write(TEST_STRING, len); |
| 36 } |
| 37 const char* expected = |
| 38 TEST_STRING TEST_STRING TEST_STRING TEST_STRING TEST_STRING |
| 39 TEST_STRING TEST_STRING TEST_STRING TEST_STRING TEST_STRING |
| 40 TEST_STRING TEST_STRING TEST_STRING TEST_STRING TEST_STRING |
| 41 TEST_STRING TEST_STRING TEST_STRING TEST_STRING TEST_STRING |
| 42 TEST_STRING TEST_STRING TEST_STRING TEST_STRING TEST_STRING |
| 43 TEST_STRING TEST_STRING TEST_STRING TEST_STRING TEST_STRING; |
| 44 const size_t expected_len = len * repeat; |
| 45 CHECK_EQ(static_cast<int>(expected_len), oss.size()); |
| 46 CHECK_GT(oss.capacity(), 0); |
| 47 CHECK_EQ(0, strncmp(expected, oss.data(), expected_len)); |
| 48 CHECK_EQ(expected, oss.c_str()); |
| 49 } |
| 50 |
| 51 |
| 52 template <class T> |
| 53 static void check(const char* expected, T value) { |
| 54 OStringStream oss; |
| 55 oss << value << " " << hex << value; |
| 56 CHECK_EQ(expected, oss.c_str()); |
| 57 } |
| 58 |
| 59 |
| 60 TEST(NumericFormatting) { |
| 61 check<bool>("0 0", false); |
| 62 check<bool>("1 1", true); |
| 63 |
| 64 check<int16_t>("-12345 cfc7", -12345); |
| 65 check<int16_t>("-32768 8000", std::numeric_limits<int16_t>::min()); |
| 66 check<int16_t>("32767 7fff", std::numeric_limits<int16_t>::max()); |
| 67 |
| 68 check<uint16_t>("34567 8707", 34567); |
| 69 check<uint16_t>("0 0", std::numeric_limits<uint16_t>::min()); |
| 70 check<uint16_t>("65535 ffff", std::numeric_limits<uint16_t>::max()); |
| 71 |
| 72 check<int32_t>("-1234567 ffed2979", -1234567); |
| 73 check<int32_t>("-2147483648 80000000", std::numeric_limits<int32_t>::min()); |
| 74 check<int32_t>("2147483647 7fffffff", std::numeric_limits<int32_t>::max()); |
| 75 |
| 76 check<uint32_t>("3456789 34bf15", 3456789); |
| 77 check<uint32_t>("0 0", std::numeric_limits<uint32_t>::min()); |
| 78 check<uint32_t>("4294967295 ffffffff", std::numeric_limits<uint32_t>::max()); |
| 79 |
| 80 check<int64_t>("-1234567 ffffffffffed2979", -1234567); |
| 81 check<int64_t>("-9223372036854775808 8000000000000000", |
| 82 std::numeric_limits<int64_t>::min()); |
| 83 check<int64_t>("9223372036854775807 7fffffffffffffff", |
| 84 std::numeric_limits<int64_t>::max()); |
| 85 |
| 86 check<uint64_t>("3456789 34bf15", 3456789); |
| 87 check<uint64_t>("0 0", std::numeric_limits<uint64_t>::min()); |
| 88 check<uint64_t>("18446744073709551615 ffffffffffffffff", |
| 89 std::numeric_limits<uint64_t>::max()); |
| 90 |
| 91 check<float>("0 0", 0.0f); |
| 92 check<float>("123 123", 123.0f); |
| 93 check<float>("-0.5 -0.5", -0.5f); |
| 94 check<float>("1.25 1.25", 1.25f); |
| 95 check<float>("0.0625 0.0625", 6.25e-2f); |
| 96 |
| 97 check<double>("0 0", 0.0); |
| 98 check<double>("123 123", 123.0); |
| 99 check<double>("-0.5 -0.5", -0.5); |
| 100 check<double>("1.25 1.25", 1.25); |
| 101 check<double>("0.0625 0.0625", 6.25e-2); |
| 102 } |
| 103 |
| 104 |
| 105 TEST(CharacterOutput) { |
| 106 check<char>("a a", 'a'); |
| 107 check<signed char>("B B", 'B'); |
| 108 check<unsigned char>("9 9", '9'); |
| 109 check<const char*>("bye bye", "bye"); |
| 110 |
| 111 OStringStream os; |
| 112 os.put('H').write("ello", 4); |
| 113 CHECK_EQ("Hello", os.c_str()); |
| 114 } |
| 115 |
| 116 |
| 117 TEST(Manipulators) { |
| 118 OStringStream os; |
| 119 os << 123 << hex << 123 << endl << 123 << dec << 123 << 123; |
| 120 CHECK_EQ("1237b\n7b123123", os.c_str()); |
| 121 } |
| 122 |
| 123 |
| 124 class MiscStuff { |
| 125 public: |
| 126 MiscStuff(int i, double d, const char* s) : i_(i), d_(d), s_(s) { } |
| 127 |
| 128 private: |
| 129 friend OStream& operator<<(OStream& os, const MiscStuff& m); |
| 130 |
| 131 int i_; |
| 132 double d_; |
| 133 const char* s_; |
| 134 }; |
| 135 |
| 136 |
| 137 OStream& operator<<(OStream& os, const MiscStuff& m) { |
| 138 return os << "{i:" << m.i_ << ", d:" << m.d_ << ", s:'" << m.s_ << "'}"; |
| 139 } |
| 140 |
| 141 |
| 142 TEST(CustomOutput) { |
| 143 OStringStream os; |
| 144 MiscStuff m(123, 4.5, "Hurz!"); |
| 145 os << m; |
| 146 CHECK_EQ("{i:123, d:4.5, s:'Hurz!'}", os.c_str()); |
| 147 } |
OLD | NEW |