1 /*
2 * Copyright 2017, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "word_stream.h"
18
19 #include "gtest/gtest.h"
20
21 #include <vector>
22
23 namespace android {
24 namespace spirit {
25
TEST(WordStreamTest,testStringOutput1)26 TEST(WordStreamTest, testStringOutput1) {
27 std::unique_ptr<OutputWordStream> OS(OutputWordStream::Create());
28 *OS << "ABCDEFG";
29 auto words = OS->getWords();
30 const std::vector<uint8_t> bytes((uint8_t *)words.data(),
31 (uint8_t *)(words.data() + words.size()));
32 const std::vector<uint8_t> bytesExpected = {0x41, 0x42, 0x43, 0x44,
33 0x45, 0x46, 0x47, 0x00};
34 EXPECT_EQ(bytesExpected, bytes);
35 }
36
TEST(WordStreamTest,testStringOutput2)37 TEST(WordStreamTest, testStringOutput2) {
38 std::unique_ptr<OutputWordStream> OS(OutputWordStream::Create());
39 *OS << "GLSL.std.450";
40 auto words = OS->getWords();
41 const std::vector<uint8_t> bytes((uint8_t *)words.data(),
42 (uint8_t *)(words.data() + words.size()));
43 const std::vector<uint8_t> bytesExpected = {
44 0x47, 0x4c, 0x53, 0x4c, 0x2e, 0x73, 0x74, 0x64,
45 0x2e, 0x34, 0x35, 0x30, 0x00, 0x00, 0x00, 0x00};
46 EXPECT_EQ(bytesExpected, bytes);
47 }
48
TEST(WordStreamTest,testStringInput1)49 TEST(WordStreamTest, testStringInput1) {
50 uint8_t bytes[] = {0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x00};
51 std::vector<uint32_t> words((uint32_t *)bytes,
52 (uint32_t *)(bytes + sizeof(bytes)));
53 std::unique_ptr<InputWordStream> IS(InputWordStream::Create(words));
54 std::string s;
55 *IS >> &s;
56 EXPECT_STREQ("ABCDEFG", s.c_str());
57 }
58
TEST(WordStreamTest,testStringInput2)59 TEST(WordStreamTest, testStringInput2) {
60 uint8_t bytes[] = {0x47, 0x4c, 0x53, 0x4c, 0x2e, 0x73, 0x74, 0x64,
61 0x2e, 0x34, 0x35, 0x30, 0x00, 0x00, 0x00, 0x00};
62 std::vector<uint32_t> words((uint32_t *)bytes,
63 (uint32_t *)(bytes + sizeof(bytes)));
64 std::unique_ptr<InputWordStream> IS(InputWordStream::Create(words));
65 std::string s;
66 *IS >> &s;
67 EXPECT_STREQ("GLSL.std.450", s.c_str());
68 }
69
70 } // namespace spirit
71 } // namespace android
72