xref: /aosp_15_r20/external/pigweed/pw_unit_test/framework_light_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2019 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 <cstring>
16 
17 #include "pw_status/status.h"
18 #include "pw_string/string_builder.h"
19 #include "pw_unit_test/framework.h"
20 
21 namespace pw {
22 
23 /// A type which compares equal to itself and FAILs if stringified.
24 struct ExplodingString {
25   friend bool operator==(const ExplodingString&, const ExplodingString&);
26 };
27 
operator ==(const ExplodingString &,const ExplodingString &)28 bool operator==(const ExplodingString&, const ExplodingString&) { return true; }
29 
30 template <>
ToString(const ExplodingString &,span<char>)31 inline StatusWithSize ToString(const ExplodingString&, span<char>) {
32   ADD_FAILURE();
33   return StatusWithSize::Unimplemented();
34 }
35 
36 namespace {
37 
TEST(UnknownTypeToString,SmallObject)38 TEST(UnknownTypeToString, SmallObject) {
39   struct {
40     char a = 0xa1;
41   } object;
42 
43   StringBuffer<64> expected;
44   expected << "<1-byte object at 0x" << &object << '>';
45   ASSERT_EQ(OkStatus(), expected.status());
46 
47   StringBuffer<64> actual;
48   actual << object;
49   ASSERT_EQ(OkStatus(), actual.status());
50   EXPECT_STREQ(expected.c_str(), actual.c_str());
51 }
52 
TEST(UnknownTypeToString,NineByteObject)53 TEST(UnknownTypeToString, NineByteObject) {
54   struct {
55     char a[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
56   } object;
57 
58   StringBuffer<64> expected;
59   expected << "<9-byte object at 0x" << &object << '>';
60   ASSERT_EQ(OkStatus(), expected.status());
61 
62   StringBuffer<64> actual;
63   actual << object;
64   ASSERT_EQ(OkStatus(), actual.status());
65   EXPECT_STREQ(expected.c_str(), actual.c_str());
66 }
67 
TEST(UnknownTypeToString,TenByteObject)68 TEST(UnknownTypeToString, TenByteObject) {
69   struct {
70     char a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
71   } object;
72 
73   StringBuffer<72> expected;
74   expected << "<10-byte object at 0x" << &object << '>';
75   ASSERT_EQ(OkStatus(), expected.status());
76 
77   StringBuffer<72> actual;
78   actual << object;
79   ASSERT_EQ(OkStatus(), actual.status());
80   EXPECT_STREQ(expected.c_str(), actual.c_str());
81 }
82 
TEST(ExpectEq,ObjectsAreNotStringifiedUnlessExpectationFails)83 TEST(ExpectEq, ObjectsAreNotStringifiedUnlessExpectationFails) {
84   EXPECT_EQ(ExplodingString{}, ExplodingString{});
85 }
86 
87 }  // namespace
88 }  // namespace pw
89