string-test.c++ 8.45 KB
Newer Older
Kenton Varda's avatar
Kenton Varda committed
1 2
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
3
//
Kenton Varda's avatar
Kenton Varda committed
4 5 6 7 8 9
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
10
//
Kenton Varda's avatar
Kenton Varda committed
11 12
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
13
//
Kenton Varda's avatar
Kenton Varda committed
14 15 16 17 18 19 20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
21

Kenton Varda's avatar
Kenton Varda committed
22
#include "string.h"
23
#include <kj/compat/gtest.h>
24
#include <string>
25
#include "vector.h"
26

27
namespace kj {
28
namespace _ {  // private
29 30
namespace {

Kenton Varda's avatar
Kenton Varda committed
31
TEST(String, Str) {
Kenton Varda's avatar
Kenton Varda committed
32 33 34 35
  EXPECT_EQ("foobar", str("foo", "bar"));
  EXPECT_EQ("1 2 3 4", str(1, " ", 2u, " ", 3l, " ", 4ll));
  EXPECT_EQ("1.5 foo 1e15 bar -3", str(1.5f, " foo ", 1e15, " bar ", -3));
  EXPECT_EQ("foo", str('f', 'o', 'o'));
36 37
  EXPECT_EQ("123 234 -123 e7",
            str((int8_t)123, " ", (uint8_t)234, " ", (int8_t)-123, " ", hex((uint8_t)0xe7)));
38 39

  char buf[3] = {'f', 'o', 'o'};
40 41 42
  ArrayPtr<char> a = buf;
  ArrayPtr<const char> ca = a;
  Vector<char> v;
43
  v.addAll(a);
44 45
  FixedArray<char, 3> f;
  memcpy(f.begin(), buf, 3);
46 47 48

  EXPECT_EQ("foo", str(a));
  EXPECT_EQ("foo", str(ca));
49
  EXPECT_EQ("foo", str(v));
50
  EXPECT_EQ("foo", str(f));
51 52
  EXPECT_EQ("foo", str(mv(a)));
  EXPECT_EQ("foo", str(mv(ca)));
53
  EXPECT_EQ("foo", str(mv(v)));
54
  EXPECT_EQ("foo", str(mv(f)));
55 56
}

57 58 59 60 61 62
TEST(String, Nullptr) {
  EXPECT_EQ(String(nullptr), "");
  EXPECT_EQ(StringPtr(String(nullptr)).size(), 0u);
  EXPECT_EQ(StringPtr(String(nullptr))[0], '\0');
}

63 64 65 66 67 68 69 70 71 72 73 74 75
TEST(String, StartsEndsWith) {
  EXPECT_TRUE(StringPtr("foobar").startsWith("foo"));
  EXPECT_FALSE(StringPtr("foobar").startsWith("bar"));
  EXPECT_FALSE(StringPtr("foobar").endsWith("foo"));
  EXPECT_TRUE(StringPtr("foobar").endsWith("bar"));

  EXPECT_FALSE(StringPtr("fo").startsWith("foo"));
  EXPECT_FALSE(StringPtr("fo").endsWith("foo"));

  EXPECT_TRUE(StringPtr("foobar").startsWith(""));
  EXPECT_TRUE(StringPtr("foobar").endsWith(""));
}

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
TEST(String, parseAs) {
  EXPECT_EQ(StringPtr("0").parseAs<double>(), 0.0);
  EXPECT_EQ(StringPtr("0.0").parseAs<double>(), 0.0);
  EXPECT_EQ(StringPtr("1").parseAs<double>(), 1.0);
  EXPECT_EQ(StringPtr("1.0").parseAs<double>(), 1.0);
  EXPECT_EQ(StringPtr("1e100").parseAs<double>(), 1e100);
  EXPECT_EQ(StringPtr("inf").parseAs<double>(), inf());
  EXPECT_EQ(StringPtr("infinity").parseAs<double>(), inf());
  EXPECT_EQ(StringPtr("INF").parseAs<double>(), inf());
  EXPECT_EQ(StringPtr("INFINITY").parseAs<double>(), inf());
  EXPECT_EQ(StringPtr("1e100000").parseAs<double>(), inf());
  EXPECT_EQ(StringPtr("-inf").parseAs<double>(), -inf());
  EXPECT_EQ(StringPtr("-infinity").parseAs<double>(), -inf());
  EXPECT_EQ(StringPtr("-INF").parseAs<double>(), -inf());
  EXPECT_EQ(StringPtr("-INFINITY").parseAs<double>(), -inf());
  EXPECT_EQ(StringPtr("-1e100000").parseAs<double>(), -inf());
  EXPECT_TRUE(isNaN(StringPtr("nan").parseAs<double>()));
  EXPECT_TRUE(isNaN(StringPtr("NAN").parseAs<double>()));
  EXPECT_TRUE(isNaN(StringPtr("NaN").parseAs<double>()));
95 96 97 98
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("not contain valid", StringPtr("").parseAs<double>());
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("not contain valid", StringPtr("a").parseAs<double>());
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("not contain valid", StringPtr("1a").parseAs<double>());
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("not contain valid", StringPtr("+-1").parseAs<double>());
99 100 101 102 103 104

  EXPECT_EQ(StringPtr("1").parseAs<float>(), 1.0);

  EXPECT_EQ(StringPtr("1").parseAs<int64_t>(), 1);
  EXPECT_EQ(StringPtr("9223372036854775807").parseAs<int64_t>(), 9223372036854775807LL);
  EXPECT_EQ(StringPtr("-9223372036854775808").parseAs<int64_t>(), -9223372036854775808ULL);
105 106 107 108 109 110
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("out-of-range", StringPtr("9223372036854775808").parseAs<int64_t>());
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("out-of-range", StringPtr("-9223372036854775809").parseAs<int64_t>());
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("not contain valid", StringPtr("").parseAs<int64_t>());
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("not contain valid", StringPtr("a").parseAs<int64_t>());
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("not contain valid", StringPtr("1a").parseAs<int64_t>());
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("not contain valid", StringPtr("+-1").parseAs<int64_t>());
111 112 113 114 115 116 117
  EXPECT_EQ(StringPtr("010").parseAs<int64_t>(), 10);
  EXPECT_EQ(StringPtr("0010").parseAs<int64_t>(), 10);
  EXPECT_EQ(StringPtr("0x10").parseAs<int64_t>(), 16);
  EXPECT_EQ(StringPtr("0X10").parseAs<int64_t>(), 16);
  EXPECT_EQ(StringPtr("-010").parseAs<int64_t>(), -10);
  EXPECT_EQ(StringPtr("-0x10").parseAs<int64_t>(), -16);
  EXPECT_EQ(StringPtr("-0X10").parseAs<int64_t>(), -16);
118 119 120 121

  EXPECT_EQ(StringPtr("1").parseAs<uint64_t>(), 1);
  EXPECT_EQ(StringPtr("0").parseAs<uint64_t>(), 0);
  EXPECT_EQ(StringPtr("18446744073709551615").parseAs<uint64_t>(), 18446744073709551615ULL);
122 123 124 125 126 127
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("out-of-range", StringPtr("-1").parseAs<uint64_t>());
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("out-of-range", StringPtr("18446744073709551616").parseAs<uint64_t>());
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("not contain valid", StringPtr("").parseAs<uint64_t>());
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("not contain valid", StringPtr("a").parseAs<uint64_t>());
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("not contain valid", StringPtr("1a").parseAs<uint64_t>());
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("not contain valid", StringPtr("+-1").parseAs<uint64_t>());
128 129 130 131

  EXPECT_EQ(StringPtr("1").parseAs<int32_t>(), 1);
  EXPECT_EQ(StringPtr("2147483647").parseAs<int32_t>(), 2147483647);
  EXPECT_EQ(StringPtr("-2147483648").parseAs<int32_t>(), -2147483648);
132 133
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("out-of-range", StringPtr("2147483648").parseAs<int32_t>());
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("out-of-range", StringPtr("-2147483649").parseAs<int32_t>());
134 135 136 137

  EXPECT_EQ(StringPtr("1").parseAs<uint32_t>(), 1);
  EXPECT_EQ(StringPtr("0").parseAs<uint32_t>(), 0U);
  EXPECT_EQ(StringPtr("4294967295").parseAs<uint32_t>(), 4294967295U);
138 139
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("out-of-range", StringPtr("-1").parseAs<uint32_t>());
  KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("out-of-range", StringPtr("4294967296").parseAs<uint32_t>());
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

  EXPECT_EQ(StringPtr("1").parseAs<int16_t>(), 1);
  EXPECT_EQ(StringPtr("1").parseAs<uint16_t>(), 1);
  EXPECT_EQ(StringPtr("1").parseAs<int8_t>(), 1);
  EXPECT_EQ(StringPtr("1").parseAs<uint8_t>(), 1);
  EXPECT_EQ(StringPtr("1").parseAs<char>(), 1);
  EXPECT_EQ(StringPtr("1").parseAs<signed char>(), 1);
  EXPECT_EQ(StringPtr("1").parseAs<unsigned char>(), 1);
  EXPECT_EQ(StringPtr("1").parseAs<short>(), 1);
  EXPECT_EQ(StringPtr("1").parseAs<unsigned short>(), 1);
  EXPECT_EQ(StringPtr("1").parseAs<int>(), 1);
  EXPECT_EQ(StringPtr("1").parseAs<unsigned>(), 1);
  EXPECT_EQ(StringPtr("1").parseAs<long>(), 1);
  EXPECT_EQ(StringPtr("1").parseAs<unsigned long>(), 1);
  EXPECT_EQ(StringPtr("1").parseAs<long long>(), 1);
  EXPECT_EQ(StringPtr("1").parseAs<unsigned long long>(), 1);

  EXPECT_EQ(heapString("1").parseAs<int>(), 1);
}

160 161 162 163 164 165 166 167 168 169 170 171
#if KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP
TEST(String, StlInterop) {
  std::string foo = "foo";
  StringPtr ptr = foo;
  EXPECT_EQ("foo", ptr);

  std::string bar = ptr;
  EXPECT_EQ("foo", bar);

  EXPECT_EQ("foo", kj::str(foo));
  EXPECT_EQ("foo", kj::heapString(foo));
}
172 173 174 175 176 177 178 179

struct Stringable {
  kj::StringPtr toString() { return "foo"; }
};

TEST(String, ToString) {
  EXPECT_EQ("foo", kj::str(Stringable()));
}
180 181
#endif

182 183 184 185 186 187 188 189 190 191 192 193
KJ_TEST("string literals with _kj suffix") {
  static constexpr StringPtr FOO = "foo"_kj;
  KJ_EXPECT(FOO == "foo", FOO);
  KJ_EXPECT(FOO[3] == 0);

  KJ_EXPECT("foo\0bar"_kj == StringPtr("foo\0bar", 7));

  static constexpr ArrayPtr<const char> ARR = "foo"_kj;
  KJ_EXPECT(ARR.size() == 3);
  KJ_EXPECT(kj::str(ARR) == "foo");
}

194
}  // namespace
195
}  // namespace _ (private)
196
}  // namespace kj