1 // Copyright 2020 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/reader/wgsl/token.h"
16
17 #include <limits>
18
19 #include "gtest/gtest.h"
20
21 namespace tint {
22 namespace reader {
23 namespace wgsl {
24 namespace {
25
26 using TokenTest = testing::Test;
27
TEST_F(TokenTest,ReturnsF32)28 TEST_F(TokenTest, ReturnsF32) {
29 Token t1(Source{}, -2.345f);
30 EXPECT_EQ(t1.to_f32(), -2.345f);
31
32 Token t2(Source{}, 2.345f);
33 EXPECT_EQ(t2.to_f32(), 2.345f);
34 }
35
TEST_F(TokenTest,ReturnsI32)36 TEST_F(TokenTest, ReturnsI32) {
37 Token t1(Source{}, -2345);
38 EXPECT_EQ(t1.to_i32(), -2345);
39
40 Token t2(Source{}, 2345);
41 EXPECT_EQ(t2.to_i32(), 2345);
42 }
43
TEST_F(TokenTest,HandlesMaxI32)44 TEST_F(TokenTest, HandlesMaxI32) {
45 Token t1(Source{}, std::numeric_limits<int32_t>::max());
46 EXPECT_EQ(t1.to_i32(), std::numeric_limits<int32_t>::max());
47 }
48
TEST_F(TokenTest,HandlesMinI32)49 TEST_F(TokenTest, HandlesMinI32) {
50 Token t1(Source{}, std::numeric_limits<int32_t>::min());
51 EXPECT_EQ(t1.to_i32(), std::numeric_limits<int32_t>::min());
52 }
53
TEST_F(TokenTest,ReturnsU32)54 TEST_F(TokenTest, ReturnsU32) {
55 Token t2(Source{}, 2345u);
56 EXPECT_EQ(t2.to_u32(), 2345u);
57 }
58
TEST_F(TokenTest,ReturnsMaxU32)59 TEST_F(TokenTest, ReturnsMaxU32) {
60 Token t1(Source{}, std::numeric_limits<uint32_t>::max());
61 EXPECT_EQ(t1.to_u32(), std::numeric_limits<uint32_t>::max());
62 }
63
TEST_F(TokenTest,Source)64 TEST_F(TokenTest, Source) {
65 Source src;
66 src.range.begin = Source::Location{3, 9};
67 src.range.end = Source::Location{4, 3};
68
69 Token t(Token::Type::kUintLiteral, src);
70 EXPECT_EQ(t.source().range.begin.line, 3u);
71 EXPECT_EQ(t.source().range.begin.column, 9u);
72 EXPECT_EQ(t.source().range.end.line, 4u);
73 EXPECT_EQ(t.source().range.end.column, 3u);
74 }
75
76 } // namespace
77 } // namespace wgsl
78 } // namespace reader
79 } // namespace tint
80