1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://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, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include <algorithm>
16 #include <array>
17 #include <cstring>
18
19 #include "pw_tokenizer/tokenize.h"
20 #include "pw_unit_test/framework.h"
21
22 namespace pw {
23 namespace tokenizer {
24 namespace {
25
26 template <size_t kSize>
TestHash(const char (& str)[kSize])27 uint32_t TestHash(const char (&str)[kSize])
28 PW_NO_SANITIZE("unsigned-integer-overflow") {
29 static_assert(kSize > 0u, "Must have at least a null terminator");
30
31 static constexpr uint32_t k65599HashConstant = 65599u;
32
33 // The length is hashed as if it were the first character.
34 uint32_t hash = kSize - 1;
35 uint32_t coefficient = k65599HashConstant;
36
37 size_t length =
38 std::min(static_cast<size_t>(PW_TOKENIZER_CFG_C_HASH_LENGTH), kSize - 1);
39
40 // Hash all of the characters in the string as unsigned ints.
41 // The coefficient calculation is done modulo 0x100000000, so the unsigned
42 // integer overflows are intentional.
43 for (size_t i = 0; i < length; ++i) {
44 hash += coefficient * str[i];
45 coefficient *= k65599HashConstant;
46 }
47
48 return hash;
49 }
50
TEST(TokenizeStringLiteral,EmptyString_IsZero)51 TEST(TokenizeStringLiteral, EmptyString_IsZero) {
52 constexpr pw_tokenizer_Token token = PW_TOKENIZE_STRING("");
53 EXPECT_EQ(0u, token);
54 }
55
TEST(TokenizeStringLiteral,String_MatchesHash)56 TEST(TokenizeStringLiteral, String_MatchesHash) {
57 constexpr uint32_t token = PW_TOKENIZE_STRING("[:-)");
58 EXPECT_EQ(TestHash("[:-)"), token);
59 }
60
61 constexpr uint32_t kGlobalToken = PW_TOKENIZE_STRING(">:-[]");
62
TEST(TokenizeStringLiteral,GlobalVariable_MatchesHash)63 TEST(TokenizeStringLiteral, GlobalVariable_MatchesHash) {
64 EXPECT_EQ(TestHash(">:-[]"), kGlobalToken);
65 }
66
67 class TokenizeToBuffer : public ::testing::Test {
68 public:
TokenizeToBuffer()69 TokenizeToBuffer() : buffer_{} {}
70
71 protected:
72 uint8_t buffer_[64];
73 };
74
75 } // namespace
76 } // namespace tokenizer
77 } // namespace pw
78