xref: /openbmc/linux/lib/kunit/assert.c (revision dc6a81c3)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Assertion and expectation serialization API.
4  *
5  * Copyright (C) 2019, Google LLC.
6  * Author: Brendan Higgins <brendanhiggins@google.com>
7  */
8 #include <kunit/assert.h>
9 
10 #include "string-stream.h"
11 
12 void kunit_base_assert_format(const struct kunit_assert *assert,
13 			      struct string_stream *stream)
14 {
15 	const char *expect_or_assert = NULL;
16 
17 	switch (assert->type) {
18 	case KUNIT_EXPECTATION:
19 		expect_or_assert = "EXPECTATION";
20 		break;
21 	case KUNIT_ASSERTION:
22 		expect_or_assert = "ASSERTION";
23 		break;
24 	}
25 
26 	string_stream_add(stream, "%s FAILED at %s:%d\n",
27 			 expect_or_assert, assert->file, assert->line);
28 }
29 EXPORT_SYMBOL_GPL(kunit_base_assert_format);
30 
31 void kunit_assert_print_msg(const struct kunit_assert *assert,
32 			    struct string_stream *stream)
33 {
34 	if (assert->message.fmt)
35 		string_stream_add(stream, "\n%pV", &assert->message);
36 }
37 EXPORT_SYMBOL_GPL(kunit_assert_print_msg);
38 
39 void kunit_fail_assert_format(const struct kunit_assert *assert,
40 			      struct string_stream *stream)
41 {
42 	kunit_base_assert_format(assert, stream);
43 	string_stream_add(stream, "%pV", &assert->message);
44 }
45 EXPORT_SYMBOL_GPL(kunit_fail_assert_format);
46 
47 void kunit_unary_assert_format(const struct kunit_assert *assert,
48 			       struct string_stream *stream)
49 {
50 	struct kunit_unary_assert *unary_assert = container_of(
51 			assert, struct kunit_unary_assert, assert);
52 
53 	kunit_base_assert_format(assert, stream);
54 	if (unary_assert->expected_true)
55 		string_stream_add(stream,
56 				 "\tExpected %s to be true, but is false\n",
57 				 unary_assert->condition);
58 	else
59 		string_stream_add(stream,
60 				 "\tExpected %s to be false, but is true\n",
61 				 unary_assert->condition);
62 	kunit_assert_print_msg(assert, stream);
63 }
64 EXPORT_SYMBOL_GPL(kunit_unary_assert_format);
65 
66 void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
67 				     struct string_stream *stream)
68 {
69 	struct kunit_ptr_not_err_assert *ptr_assert = container_of(
70 			assert, struct kunit_ptr_not_err_assert, assert);
71 
72 	kunit_base_assert_format(assert, stream);
73 	if (!ptr_assert->value) {
74 		string_stream_add(stream,
75 				 "\tExpected %s is not null, but is\n",
76 				 ptr_assert->text);
77 	} else if (IS_ERR(ptr_assert->value)) {
78 		string_stream_add(stream,
79 				 "\tExpected %s is not error, but is: %ld\n",
80 				 ptr_assert->text,
81 				 PTR_ERR(ptr_assert->value));
82 	}
83 	kunit_assert_print_msg(assert, stream);
84 }
85 EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);
86 
87 void kunit_binary_assert_format(const struct kunit_assert *assert,
88 				struct string_stream *stream)
89 {
90 	struct kunit_binary_assert *binary_assert = container_of(
91 			assert, struct kunit_binary_assert, assert);
92 
93 	kunit_base_assert_format(assert, stream);
94 	string_stream_add(stream,
95 			 "\tExpected %s %s %s, but\n",
96 			 binary_assert->left_text,
97 			 binary_assert->operation,
98 			 binary_assert->right_text);
99 	string_stream_add(stream, "\t\t%s == %lld\n",
100 			 binary_assert->left_text,
101 			 binary_assert->left_value);
102 	string_stream_add(stream, "\t\t%s == %lld",
103 			 binary_assert->right_text,
104 			 binary_assert->right_value);
105 	kunit_assert_print_msg(assert, stream);
106 }
107 EXPORT_SYMBOL_GPL(kunit_binary_assert_format);
108 
109 void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
110 				    struct string_stream *stream)
111 {
112 	struct kunit_binary_ptr_assert *binary_assert = container_of(
113 			assert, struct kunit_binary_ptr_assert, assert);
114 
115 	kunit_base_assert_format(assert, stream);
116 	string_stream_add(stream,
117 			 "\tExpected %s %s %s, but\n",
118 			 binary_assert->left_text,
119 			 binary_assert->operation,
120 			 binary_assert->right_text);
121 	string_stream_add(stream, "\t\t%s == %pK\n",
122 			 binary_assert->left_text,
123 			 binary_assert->left_value);
124 	string_stream_add(stream, "\t\t%s == %pK",
125 			 binary_assert->right_text,
126 			 binary_assert->right_value);
127 	kunit_assert_print_msg(assert, stream);
128 }
129 EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format);
130 
131 void kunit_binary_str_assert_format(const struct kunit_assert *assert,
132 				    struct string_stream *stream)
133 {
134 	struct kunit_binary_str_assert *binary_assert = container_of(
135 			assert, struct kunit_binary_str_assert, assert);
136 
137 	kunit_base_assert_format(assert, stream);
138 	string_stream_add(stream,
139 			 "\tExpected %s %s %s, but\n",
140 			 binary_assert->left_text,
141 			 binary_assert->operation,
142 			 binary_assert->right_text);
143 	string_stream_add(stream, "\t\t%s == %s\n",
144 			 binary_assert->left_text,
145 			 binary_assert->left_value);
146 	string_stream_add(stream, "\t\t%s == %s",
147 			 binary_assert->right_text,
148 			 binary_assert->right_value);
149 	kunit_assert_print_msg(assert, stream);
150 }
151 EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);
152