Alex Vakulenko | b6513a1 | 2014-05-05 17:23:40 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS 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> |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | #include "buffet/any.h" |
| 10 | |
| 11 | using buffet::internal_details::Buffer; |
| 12 | |
| 13 | TEST(Buffer, Empty) { |
| 14 | Buffer buffer; |
| 15 | EXPECT_TRUE(buffer.IsEmpty()); |
| 16 | EXPECT_EQ(Buffer::kExternal, buffer.storage_); |
| 17 | EXPECT_EQ(nullptr, buffer.GetDataPtr()); |
| 18 | } |
| 19 | |
| 20 | TEST(Buffer, Store_Int) { |
| 21 | Buffer buffer; |
| 22 | buffer.Assign(2); |
| 23 | EXPECT_FALSE(buffer.IsEmpty()); |
| 24 | EXPECT_EQ(Buffer::kContained, buffer.storage_); |
| 25 | EXPECT_EQ(typeid(int), buffer.GetDataPtr()->GetType()); |
| 26 | } |
| 27 | |
| 28 | TEST(Buffer, Store_Double) { |
| 29 | Buffer buffer; |
| 30 | buffer.Assign(2.3); |
| 31 | EXPECT_FALSE(buffer.IsEmpty()); |
| 32 | EXPECT_EQ(Buffer::kContained, buffer.storage_); |
| 33 | EXPECT_EQ(typeid(double), buffer.GetDataPtr()->GetType()); |
| 34 | } |
| 35 | |
| 36 | TEST(Buffer, Store_Pointers) { |
| 37 | Buffer buffer; |
| 38 | // nullptr |
| 39 | buffer.Assign(nullptr); |
| 40 | EXPECT_FALSE(buffer.IsEmpty()); |
| 41 | EXPECT_EQ(Buffer::kContained, buffer.storage_); |
| 42 | EXPECT_EQ(typeid(nullptr_t), buffer.GetDataPtr()->GetType()); |
| 43 | |
| 44 | // char * |
| 45 | buffer.Assign("abcd"); |
| 46 | EXPECT_FALSE(buffer.IsEmpty()); |
| 47 | EXPECT_EQ(Buffer::kContained, buffer.storage_); |
| 48 | EXPECT_EQ(typeid(const char*), buffer.GetDataPtr()->GetType()); |
| 49 | |
| 50 | // pointer to non-trivial object |
| 51 | class NonTrivial { |
| 52 | public: |
| 53 | virtual ~NonTrivial() {} |
| 54 | } non_trivial; |
| 55 | buffer.Assign(&non_trivial); |
| 56 | EXPECT_FALSE(buffer.IsEmpty()); |
| 57 | EXPECT_EQ(Buffer::kContained, buffer.storage_); |
| 58 | EXPECT_EQ(typeid(NonTrivial*), buffer.GetDataPtr()->GetType()); |
| 59 | } |
| 60 | |
| 61 | TEST(Buffer, Store_NonTrivialObjects) { |
| 62 | class NonTrivial { |
| 63 | public: |
| 64 | virtual ~NonTrivial() {} |
| 65 | } non_trivial; |
| 66 | Buffer buffer; |
| 67 | buffer.Assign(non_trivial); |
| 68 | EXPECT_FALSE(buffer.IsEmpty()); |
| 69 | EXPECT_EQ(Buffer::kExternal, buffer.storage_); |
| 70 | EXPECT_EQ(typeid(NonTrivial), buffer.GetDataPtr()->GetType()); |
| 71 | } |
| 72 | |
| 73 | TEST(Buffer, Store_Objects) { |
| 74 | Buffer buffer; |
| 75 | |
| 76 | struct Small { |
| 77 | double d; |
| 78 | } small = {}; |
| 79 | buffer.Assign(small); |
| 80 | EXPECT_FALSE(buffer.IsEmpty()); |
| 81 | EXPECT_EQ(Buffer::kContained, buffer.storage_); |
| 82 | EXPECT_EQ(typeid(Small), buffer.GetDataPtr()->GetType()); |
| 83 | |
| 84 | struct Large { |
| 85 | char c[10]; |
| 86 | } large = {}; |
| 87 | buffer.Assign(large); |
| 88 | EXPECT_FALSE(buffer.IsEmpty()); |
| 89 | EXPECT_EQ(Buffer::kExternal, buffer.storage_); |
| 90 | EXPECT_EQ(typeid(Large), buffer.GetDataPtr()->GetType()); |
| 91 | } |
| 92 | |
| 93 | TEST(Buffer, Copy) { |
| 94 | Buffer buffer1; |
| 95 | Buffer buffer2; |
| 96 | |
| 97 | buffer1.Assign(30); |
| 98 | buffer1.CopyTo(&buffer2); |
| 99 | EXPECT_FALSE(buffer1.IsEmpty()); |
| 100 | EXPECT_FALSE(buffer2.IsEmpty()); |
| 101 | EXPECT_EQ(typeid(int), buffer1.GetDataPtr()->GetType()); |
| 102 | EXPECT_EQ(typeid(int), buffer2.GetDataPtr()->GetType()); |
| 103 | EXPECT_EQ(30, buffer1.GetData<int>()); |
| 104 | EXPECT_EQ(30, buffer2.GetData<int>()); |
| 105 | |
| 106 | buffer1.Assign(std::string("abc")); |
| 107 | buffer1.CopyTo(&buffer2); |
| 108 | EXPECT_FALSE(buffer1.IsEmpty()); |
| 109 | EXPECT_FALSE(buffer2.IsEmpty()); |
| 110 | EXPECT_EQ(typeid(std::string), buffer1.GetDataPtr()->GetType()); |
| 111 | EXPECT_EQ(typeid(std::string), buffer2.GetDataPtr()->GetType()); |
| 112 | EXPECT_EQ("abc", buffer1.GetData<std::string>()); |
| 113 | EXPECT_EQ("abc", buffer2.GetData<std::string>()); |
| 114 | } |