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::tokenizer {
23 namespace {
24
25 template <size_t kSize>
TestHash(const char (& str)[kSize])26 uint32_t TestHash(const char (&str)[kSize])
27 PW_NO_SANITIZE("unsigned-integer-overflow") {
28 static_assert(kSize > 0u, "Must have at least a null terminator");
29
30 static constexpr uint32_t k65599HashConstant = 65599u;
31
32 // The length is hashed as if it were the first character.
33 uint32_t hash = kSize - 1;
34 uint32_t coefficient = k65599HashConstant;
35
36 size_t length =
37 std::min(static_cast<size_t>(PW_TOKENIZER_CFG_C_HASH_LENGTH), kSize - 1);
38
39 // Hash all of the characters in the string as unsigned ints.
40 // The coefficient calculation is done modulo 0x100000000, so the unsigned
41 // integer overflows are intentional.
42 for (size_t i = 0; i < length; ++i) {
43 hash += coefficient * str[i];
44 coefficient *= k65599HashConstant;
45 }
46
47 return hash;
48 }
49
TEST(TokenizeStringLiteral,EmptyString_IsZero)50 TEST(TokenizeStringLiteral, EmptyString_IsZero) {
51 constexpr pw_tokenizer_Token token = PW_TOKENIZE_STRING("");
52 EXPECT_EQ(0u, token);
53 }
54
TEST(TokenizeStringLiteral,String_MatchesHash)55 TEST(TokenizeStringLiteral, String_MatchesHash) {
56 constexpr uint32_t token = PW_TOKENIZE_STRING("[:-)");
57 EXPECT_EQ(TestHash("[:-)"), token);
58 }
59
60 constexpr uint32_t kGlobalToken = PW_TOKENIZE_STRING(">:-[]");
61
TEST(TokenizeStringLiteral,GlobalVariable_MatchesHash)62 TEST(TokenizeStringLiteral, GlobalVariable_MatchesHash) {
63 EXPECT_EQ(TestHash(">:-[]"), kGlobalToken);
64 }
65
66 class TokenizeToBuffer : public ::testing::Test {
67 public:
TokenizeToBuffer()68 TokenizeToBuffer() : buffer_{} {}
69
70 protected:
71 uint8_t buffer_[64];
72 };
73
74 } // namespace
75 } // namespace pw::tokenizer
76