@@ -122,3 +122,63 @@ TEST_CASE("pointer_object_exprt", "[core][util]")
122122 }
123123 }
124124}
125+
126+ TEST_CASE (" object_size_exprt" , " [core][util]" )
127+ {
128+ const exprt pointer = symbol_exprt{" foo" , pointer_type (void_type ())};
129+ const object_size_exprt object_size{pointer, size_type ()};
130+ SECTION (" Is equivalent to free function." )
131+ {
132+ CHECK (::object_size (pointer) == object_size);
133+ }
134+ SECTION (" Result type" )
135+ {
136+ CHECK (object_size.type () == size_type ());
137+ }
138+ SECTION (" Pointer operand accessor" )
139+ {
140+ CHECK (object_size.pointer () == pointer);
141+ }
142+ SECTION (" Downcasting" )
143+ {
144+ const exprt &upcast = object_size;
145+ CHECK (expr_try_dynamic_cast<object_size_exprt>(upcast));
146+ CHECK_FALSE (expr_try_dynamic_cast<pointer_object_exprt>(upcast));
147+ SECTION (" Validation" )
148+ {
149+ SECTION (" Invalid operand" )
150+ {
151+ unary_exprt invalid_type = object_size;
152+ invalid_type.op ().type () = bool_typet{};
153+ const cbmc_invariants_should_throwt invariants_throw;
154+ REQUIRE_THROWS_MATCHES (
155+ expr_try_dynamic_cast<object_size_exprt>(invalid_type),
156+ invariant_failedt,
157+ invariant_failure_containing (
158+ " Object size expression must have pointer typed operand." ));
159+ }
160+ SECTION (" Missing operand" )
161+ {
162+ exprt missing_operand = object_size;
163+ missing_operand.operands ().clear ();
164+ const cbmc_invariants_should_throwt invariants_throw;
165+ REQUIRE_THROWS_MATCHES (
166+ expr_try_dynamic_cast<object_size_exprt>(missing_operand),
167+ invariant_failedt,
168+ invariant_failure_containing (
169+ " Object size expression must have one operand" ));
170+ }
171+ SECTION (" Too many operands" )
172+ {
173+ exprt two_operands = object_size;
174+ two_operands.operands ().push_back (pointer);
175+ const cbmc_invariants_should_throwt invariants_throw;
176+ REQUIRE_THROWS_MATCHES (
177+ expr_try_dynamic_cast<object_size_exprt>(two_operands),
178+ invariant_failedt,
179+ invariant_failure_containing (
180+ " Object size expression must have one operand" ));
181+ }
182+ }
183+ }
184+ }
0 commit comments