1 // Copyright 2018 The ANGLE 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 // ImmutableString_test.cpp:
6 //   Tests for ImmutableString and ImmutableStringBuilder.
7 
8 #include "compiler/translator/ImmutableString.h"
9 #include "compiler/translator/ImmutableStringBuilder.h"
10 #include "compiler/translator/PoolAlloc.h"
11 #include "gtest/gtest.h"
12 
13 using namespace sh;
14 
15 class ImmutableStringBuilderTest : public testing::Test
16 {
17   public:
ImmutableStringBuilderTest()18     ImmutableStringBuilderTest() {}
19 
20   protected:
SetUp()21     void SetUp() override
22     {
23         allocator.push();
24         SetGlobalPoolAllocator(&allocator);
25     }
26 
TearDown()27     void TearDown() override
28     {
29         SetGlobalPoolAllocator(nullptr);
30         allocator.pop();
31     }
32 
33     angle::PoolAllocator allocator;
34 };
35 
36 // Test writing a 32-bit signed int as hexadecimal using ImmutableStringBuilder.
TEST_F(ImmutableStringBuilderTest,AppendHexInt32)37 TEST_F(ImmutableStringBuilderTest, AppendHexInt32)
38 {
39     int32_t i = -1;
40     ImmutableStringBuilder strBuilder(2 * sizeof(int32_t));
41     strBuilder.appendHex(i);
42     ImmutableString str = strBuilder;
43     EXPECT_EQ(std::string("ffffffff"), str.data());
44 }
45 
46 // Test writing a 32-bit unsigned int as hexadecimal using ImmutableStringBuilder.
TEST_F(ImmutableStringBuilderTest,AppendHexUint32)47 TEST_F(ImmutableStringBuilderTest, AppendHexUint32)
48 {
49     uint32_t i = 0x1234beefu;
50     ImmutableStringBuilder strBuilder(2 * sizeof(uint32_t));
51     strBuilder.appendHex(i);
52     ImmutableString str = strBuilder;
53     EXPECT_EQ(std::string("1234beef"), str.data());
54 }
55 
56 // Test writing a 64-bit signed int as hexadecimal using ImmutableStringBuilder.
TEST_F(ImmutableStringBuilderTest,AppendHexInt64)57 TEST_F(ImmutableStringBuilderTest, AppendHexInt64)
58 {
59     int64_t i = -1;
60     ImmutableStringBuilder strBuilder(2 * sizeof(int64_t));
61     strBuilder.appendHex(i);
62     ImmutableString str = strBuilder;
63     EXPECT_EQ(std::string("ffffffffffffffff"), str.data());
64 }
65 
66 // Test writing a 64-bit unsigned int as hexadecimal using ImmutableStringBuilder.
TEST_F(ImmutableStringBuilderTest,AppendHexUint64)67 TEST_F(ImmutableStringBuilderTest, AppendHexUint64)
68 {
69     uint64_t i = 0xfeedcafe9876beefull;
70     ImmutableStringBuilder strBuilder(2 * sizeof(uint64_t));
71     strBuilder.appendHex(i);
72     ImmutableString str = strBuilder;
73     EXPECT_EQ(std::string("feedcafe9876beef"), str.data());
74 }
75