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