blob-test.c++ (4579B)
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors 2 // Licensed under the MIT License: 3 // 4 // Permission is hereby granted, free of charge, to any person obtaining a copy 5 // of this software and associated documentation files (the "Software"), to deal 6 // in the Software without restriction, including without limitation the rights 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 // copies of the Software, and to permit persons to whom the Software is 9 // furnished to do so, subject to the following conditions: 10 // 11 // The above copyright notice and this permission notice shall be included in 12 // all copies or substantial portions of the Software. 13 // 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 // THE SOFTWARE. 21 22 #include "blob.h" 23 #include <kj/compat/gtest.h> 24 #include <iostream> 25 #include <string> 26 #include "test-util.h" 27 28 // TODO(test): This test is outdated -- it predates the retrofit of Text and Data on top of 29 // kj::ArrayPtr/kj::StringPtr. Clean it up. 30 31 namespace capnp { 32 namespace { 33 34 TEST(Blob, Text) { 35 std::string str = "foo"; 36 Text::Reader text = str.c_str(); 37 38 EXPECT_EQ("foo", text); 39 EXPECT_STREQ("foo", text.cStr()); 40 EXPECT_STREQ("foo", text.begin()); 41 EXPECT_EQ(3u, text.size()); 42 43 Text::Reader text2 = "bar"; 44 EXPECT_EQ("bar", text2); 45 46 char c[4] = "baz"; 47 Text::Reader text3(c); 48 EXPECT_EQ("baz", text3); 49 50 Text::Builder builder(c, 3); 51 EXPECT_EQ("baz", builder); 52 53 EXPECT_EQ(kj::arrayPtr("az", 2), builder.slice(1, 3)); 54 } 55 56 Data::Reader dataLit(const char* str) { 57 return Data::Reader(reinterpret_cast<const byte*>(str), strlen(str)); 58 } 59 60 TEST(Blob, Data) { 61 Data::Reader data = dataLit("foo"); 62 63 EXPECT_EQ(dataLit("foo"), data); 64 EXPECT_EQ(3u, data.size()); 65 66 Data::Reader data2 = dataLit("bar"); 67 EXPECT_EQ(dataLit("bar"), data2); 68 69 byte c[4] = "baz"; 70 Data::Reader data3(c, 3); 71 EXPECT_EQ(dataLit("baz"), data3); 72 73 Data::Builder builder(c, 3); 74 EXPECT_EQ(dataLit("baz"), builder); 75 76 EXPECT_EQ(dataLit("az"), builder.slice(1, 3)); 77 } 78 79 TEST(Blob, Compare) { 80 EXPECT_TRUE (Text::Reader("foo") == Text::Reader("foo")); 81 EXPECT_FALSE(Text::Reader("foo") != Text::Reader("foo")); 82 EXPECT_TRUE (Text::Reader("foo") <= Text::Reader("foo")); 83 EXPECT_TRUE (Text::Reader("foo") >= Text::Reader("foo")); 84 EXPECT_FALSE(Text::Reader("foo") < Text::Reader("foo")); 85 EXPECT_FALSE(Text::Reader("foo") > Text::Reader("foo")); 86 87 EXPECT_FALSE(Text::Reader("foo") == Text::Reader("bar")); 88 EXPECT_TRUE (Text::Reader("foo") != Text::Reader("bar")); 89 EXPECT_FALSE(Text::Reader("foo") <= Text::Reader("bar")); 90 EXPECT_TRUE (Text::Reader("foo") >= Text::Reader("bar")); 91 EXPECT_FALSE(Text::Reader("foo") < Text::Reader("bar")); 92 EXPECT_TRUE (Text::Reader("foo") > Text::Reader("bar")); 93 94 EXPECT_FALSE(Text::Reader("bar") == Text::Reader("foo")); 95 EXPECT_TRUE (Text::Reader("bar") != Text::Reader("foo")); 96 EXPECT_TRUE (Text::Reader("bar") <= Text::Reader("foo")); 97 EXPECT_FALSE(Text::Reader("bar") >= Text::Reader("foo")); 98 EXPECT_TRUE (Text::Reader("bar") < Text::Reader("foo")); 99 EXPECT_FALSE(Text::Reader("bar") > Text::Reader("foo")); 100 101 EXPECT_FALSE(Text::Reader("foobar") == Text::Reader("foo")); 102 EXPECT_TRUE (Text::Reader("foobar") != Text::Reader("foo")); 103 EXPECT_FALSE(Text::Reader("foobar") <= Text::Reader("foo")); 104 EXPECT_TRUE (Text::Reader("foobar") >= Text::Reader("foo")); 105 EXPECT_FALSE(Text::Reader("foobar") < Text::Reader("foo")); 106 EXPECT_TRUE (Text::Reader("foobar") > Text::Reader("foo")); 107 108 EXPECT_FALSE(Text::Reader("foo") == Text::Reader("foobar")); 109 EXPECT_TRUE (Text::Reader("foo") != Text::Reader("foobar")); 110 EXPECT_TRUE (Text::Reader("foo") <= Text::Reader("foobar")); 111 EXPECT_FALSE(Text::Reader("foo") >= Text::Reader("foobar")); 112 EXPECT_TRUE (Text::Reader("foo") < Text::Reader("foobar")); 113 EXPECT_FALSE(Text::Reader("foo") > Text::Reader("foobar")); 114 } 115 116 #if KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP 117 TEST(Blob, StlInterop) { 118 std::string foo = "foo"; 119 Text::Reader reader = foo; 120 121 EXPECT_EQ("foo", reader); 122 123 std::string bar = reader; 124 EXPECT_EQ("foo", bar); 125 } 126 #endif 127 128 } // namespace 129 } // namespace capnp