xref: /aosp_15_r20/external/stg/hex_test.cc (revision 9e3b08ae94a55201065475453d799e8b1378bea6)
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- mode: C++ -*-
3 //
4 // Copyright 2024 Google LLC
5 //
6 // Licensed under the Apache License v2.0 with LLVM Exceptions (the
7 // "License"); you may not use this file except in compliance with the
8 // License.  You may obtain a copy of the License at
9 //
10 //     https://llvm.org/LICENSE.txt
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
18 // Author: Giuliano Procida
19 
20 #include "hex.h"
21 
22 #include <cstdint>
23 #include <sstream>
24 #include <string_view>
25 
26 #include <catch2/catch.hpp>
27 
28 namespace Test {
29 
30 struct TestCase {
31   std::string_view name;
32   int value;
33   std::string_view formatted;
34 };
35 
36 TEST_CASE("Hex<uint32_t>") {
37   const auto test = GENERATE(
38       TestCase({"zero", 0, "0x0"}),
39       TestCase({"half width", 0xabcd, "0xabcd"}),
40       TestCase({"full width", 0x12345678, "0x12345678"}));
41 
42   INFO("testing with " << test.name << " value");
43   std::ostringstream os;
44   os << stg::Hex<uint32_t>(test.value);
45   CHECK(os.str() == test.formatted);
46 }
47 
48 TEST_CASE("self comparison") {
49   const stg::Hex<uint8_t> a(0);
50   CHECK(a == a);
51   CHECK(!(a != a));
52   CHECK(!(a < a));
53   CHECK(a <= a);
54   CHECK(!(a > a));
55   CHECK(a >= a);
56 }
57 
58 TEST_CASE("distinct comparison") {
59   const stg::Hex<uint8_t> a(0);
60   const stg::Hex<uint8_t> b(1);
61   CHECK(!(a == b));
62   CHECK(a != b);
63   CHECK(a < b);
64   CHECK(a <= b);
65   CHECK(!(a > b));
66   CHECK(!(a >= b));
67 }
68 
69 }  // namespace Test
70