libstdc++: Refactor loops in std::__platform_semaphore
[official-gcc.git] / gcc / selftest-json.cc
blob4f52a87538ae9c0f798c25e0daa4988153016a8b
1 /* Selftest support for JSON.
2 Copyright (C) 2024 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_MEMORY
23 #include "system.h"
24 #include "coretypes.h"
25 #include "diagnostic.h"
26 #include "selftest.h"
27 #include "selftest-json.h"
29 /* The selftest code should entirely disappear in a production
30 configuration, hence we guard all of it with #if CHECKING_P. */
32 #if CHECKING_P
34 namespace selftest {
36 /* Assert that VALUE is a non-null json::string
37 equalling EXPECTED_VALUE.
38 Use LOC for any failures. */
40 void
41 assert_json_string_eq (const location &loc,
42 const json::value *value,
43 const char *expected_value)
45 ASSERT_EQ_AT (loc, value->get_kind (), json::JSON_STRING);
46 const json::string *str = static_cast<const json::string *> (value);
47 ASSERT_STREQ_AT (loc, expected_value, str->get_string ());
50 /* Assert that VALUE is a non-null json::object,
51 returning it as such, failing at LOC if this isn't the case. */
53 const json::object *
54 expect_json_object (const location &loc,
55 const json::value *value)
57 ASSERT_NE_AT (loc, value, nullptr);
58 ASSERT_EQ_AT (loc, value->get_kind (), json::JSON_OBJECT);
59 return static_cast<const json::object *> (value);
62 /* Assert that VALUE is a non-null json::object that has property
63 PROPERTY_NAME.
64 Return the value of the property.
65 Use LOC for any failures. */
67 const json::value *
68 expect_json_object_with_property (const location &loc,
69 const json::value *value,
70 const char *property_name)
72 const json::object *obj = expect_json_object (loc, value);
73 const json::value *property_value = obj->get (property_name);
74 ASSERT_NE_AT (loc, property_value, nullptr);
75 return property_value;
78 /* Assert that VALUE is a non-null json::object that has property
79 PROPERTY_NAME, and that the value of that property is a non-null
80 json::integer_number equalling EXPECTED_VALUE.
81 Use LOC for any failures. */
83 void
84 assert_json_int_property_eq (const location &loc,
85 const json::value *value,
86 const char *property_name,
87 long expected_value)
89 const json::value *property_value
90 = expect_json_object_with_property (loc, value, property_name);
91 ASSERT_EQ_AT (loc, property_value->get_kind (), json::JSON_INTEGER);
92 long actual_value
93 = static_cast<const json::integer_number *> (property_value)->get ();
94 ASSERT_EQ_AT (loc, expected_value, actual_value);
97 /* Assert that VALUE is a non-null json::object that has property
98 PROPERTY_NAME, and that the property value is a non-null JSON object.
99 Return the value of the property as a json::object.
100 Use LOC for any failures. */
102 const json::object *
103 expect_json_object_with_object_property (const location &loc,
104 const json::value *value,
105 const char *property_name)
107 const json::value *property_value
108 = expect_json_object_with_property (loc, value, property_name);
109 ASSERT_EQ_AT (loc, property_value->get_kind (), json::JSON_OBJECT);
110 return static_cast<const json::object *> (property_value);
113 /* Assert that VALUE is a non-null json::object that has property
114 PROPERTY_NAME, and that the property value is a non-null JSON array.
115 Return the value of the property as a json::array.
116 Use LOC for any failures. */
118 const json::array *
119 expect_json_object_with_array_property (const location &loc,
120 const json::value *value,
121 const char *property_name)
123 const json::value *property_value
124 = expect_json_object_with_property (loc, value, property_name);
125 ASSERT_EQ_AT (loc, property_value->get_kind (), json::JSON_ARRAY);
126 return static_cast<const json::array *> (property_value);
129 /* Assert that VALUE is a non-null json::object that has property
130 PROPERTY_NAME, and that the property value is a non-null JSON string.
131 Return the value of the property as a json::string.
132 Use LOC for any failures. */
134 const json::string *
135 expect_json_object_with_string_property (const location &loc,
136 const json::value *value,
137 const char *property_name)
139 const json::value *property_value
140 = expect_json_object_with_property (loc, value, property_name);
141 ASSERT_EQ_AT (loc, property_value->get_kind (), json::JSON_STRING);
142 return static_cast<const json::string *> (property_value);
145 /* Assert that VALUE is a non-null json::object that has property
146 PROPERTY_NAME, and that the value of that property is a non-null
147 JSON string equalling EXPECTED_VALUE.
148 Use LOC for any failures. */
150 void
151 assert_json_string_property_eq (const location &loc,
152 const json::value *value,
153 const char *property_name,
154 const char *expected_value)
156 const json::value *property_value
157 = expect_json_object_with_property (loc, value, property_name);
158 assert_json_string_eq (loc, property_value, expected_value);
161 } // namespace selftest
163 #endif /* #if CHECKING_P */