18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Assertion and expectation serialization API.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2019, Google LLC.
68c2ecf20Sopenharmony_ci * Author: Brendan Higgins <brendanhiggins@google.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci#include <kunit/assert.h>
98c2ecf20Sopenharmony_ci#include <kunit/test.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include "string-stream.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_civoid kunit_base_assert_format(const struct kunit_assert *assert,
148c2ecf20Sopenharmony_ci			      struct string_stream *stream)
158c2ecf20Sopenharmony_ci{
168c2ecf20Sopenharmony_ci	const char *expect_or_assert = NULL;
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci	switch (assert->type) {
198c2ecf20Sopenharmony_ci	case KUNIT_EXPECTATION:
208c2ecf20Sopenharmony_ci		expect_or_assert = "EXPECTATION";
218c2ecf20Sopenharmony_ci		break;
228c2ecf20Sopenharmony_ci	case KUNIT_ASSERTION:
238c2ecf20Sopenharmony_ci		expect_or_assert = "ASSERTION";
248c2ecf20Sopenharmony_ci		break;
258c2ecf20Sopenharmony_ci	}
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	string_stream_add(stream, "%s FAILED at %s:%d\n",
288c2ecf20Sopenharmony_ci			 expect_or_assert, assert->file, assert->line);
298c2ecf20Sopenharmony_ci}
308c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(kunit_base_assert_format);
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_civoid kunit_assert_print_msg(const struct kunit_assert *assert,
338c2ecf20Sopenharmony_ci			    struct string_stream *stream)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	if (assert->message.fmt)
368c2ecf20Sopenharmony_ci		string_stream_add(stream, "\n%pV", &assert->message);
378c2ecf20Sopenharmony_ci}
388c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(kunit_assert_print_msg);
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_civoid kunit_fail_assert_format(const struct kunit_assert *assert,
418c2ecf20Sopenharmony_ci			      struct string_stream *stream)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	kunit_base_assert_format(assert, stream);
448c2ecf20Sopenharmony_ci	string_stream_add(stream, "%pV", &assert->message);
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(kunit_fail_assert_format);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_civoid kunit_unary_assert_format(const struct kunit_assert *assert,
498c2ecf20Sopenharmony_ci			       struct string_stream *stream)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	struct kunit_unary_assert *unary_assert = container_of(
528c2ecf20Sopenharmony_ci			assert, struct kunit_unary_assert, assert);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	kunit_base_assert_format(assert, stream);
558c2ecf20Sopenharmony_ci	if (unary_assert->expected_true)
568c2ecf20Sopenharmony_ci		string_stream_add(stream,
578c2ecf20Sopenharmony_ci				  KUNIT_SUBTEST_INDENT "Expected %s to be true, but is false\n",
588c2ecf20Sopenharmony_ci				  unary_assert->condition);
598c2ecf20Sopenharmony_ci	else
608c2ecf20Sopenharmony_ci		string_stream_add(stream,
618c2ecf20Sopenharmony_ci				  KUNIT_SUBTEST_INDENT "Expected %s to be false, but is true\n",
628c2ecf20Sopenharmony_ci				  unary_assert->condition);
638c2ecf20Sopenharmony_ci	kunit_assert_print_msg(assert, stream);
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(kunit_unary_assert_format);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_civoid kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
688c2ecf20Sopenharmony_ci				     struct string_stream *stream)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	struct kunit_ptr_not_err_assert *ptr_assert = container_of(
718c2ecf20Sopenharmony_ci			assert, struct kunit_ptr_not_err_assert, assert);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	kunit_base_assert_format(assert, stream);
748c2ecf20Sopenharmony_ci	if (!ptr_assert->value) {
758c2ecf20Sopenharmony_ci		string_stream_add(stream,
768c2ecf20Sopenharmony_ci				  KUNIT_SUBTEST_INDENT "Expected %s is not null, but is\n",
778c2ecf20Sopenharmony_ci				  ptr_assert->text);
788c2ecf20Sopenharmony_ci	} else if (IS_ERR(ptr_assert->value)) {
798c2ecf20Sopenharmony_ci		string_stream_add(stream,
808c2ecf20Sopenharmony_ci				  KUNIT_SUBTEST_INDENT "Expected %s is not error, but is: %ld\n",
818c2ecf20Sopenharmony_ci				  ptr_assert->text,
828c2ecf20Sopenharmony_ci				  PTR_ERR(ptr_assert->value));
838c2ecf20Sopenharmony_ci	}
848c2ecf20Sopenharmony_ci	kunit_assert_print_msg(assert, stream);
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_civoid kunit_binary_assert_format(const struct kunit_assert *assert,
898c2ecf20Sopenharmony_ci				struct string_stream *stream)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	struct kunit_binary_assert *binary_assert = container_of(
928c2ecf20Sopenharmony_ci			assert, struct kunit_binary_assert, assert);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	kunit_base_assert_format(assert, stream);
958c2ecf20Sopenharmony_ci	string_stream_add(stream,
968c2ecf20Sopenharmony_ci			  KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n",
978c2ecf20Sopenharmony_ci			  binary_assert->left_text,
988c2ecf20Sopenharmony_ci			  binary_assert->operation,
998c2ecf20Sopenharmony_ci			  binary_assert->right_text);
1008c2ecf20Sopenharmony_ci	string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld\n",
1018c2ecf20Sopenharmony_ci			  binary_assert->left_text,
1028c2ecf20Sopenharmony_ci			  binary_assert->left_value);
1038c2ecf20Sopenharmony_ci	string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld",
1048c2ecf20Sopenharmony_ci			  binary_assert->right_text,
1058c2ecf20Sopenharmony_ci			  binary_assert->right_value);
1068c2ecf20Sopenharmony_ci	kunit_assert_print_msg(assert, stream);
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(kunit_binary_assert_format);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_civoid kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
1118c2ecf20Sopenharmony_ci				    struct string_stream *stream)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	struct kunit_binary_ptr_assert *binary_assert = container_of(
1148c2ecf20Sopenharmony_ci			assert, struct kunit_binary_ptr_assert, assert);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	kunit_base_assert_format(assert, stream);
1178c2ecf20Sopenharmony_ci	string_stream_add(stream,
1188c2ecf20Sopenharmony_ci			  KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n",
1198c2ecf20Sopenharmony_ci			  binary_assert->left_text,
1208c2ecf20Sopenharmony_ci			  binary_assert->operation,
1218c2ecf20Sopenharmony_ci			  binary_assert->right_text);
1228c2ecf20Sopenharmony_ci	string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %px\n",
1238c2ecf20Sopenharmony_ci			  binary_assert->left_text,
1248c2ecf20Sopenharmony_ci			  binary_assert->left_value);
1258c2ecf20Sopenharmony_ci	string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %px",
1268c2ecf20Sopenharmony_ci			  binary_assert->right_text,
1278c2ecf20Sopenharmony_ci			  binary_assert->right_value);
1288c2ecf20Sopenharmony_ci	kunit_assert_print_msg(assert, stream);
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_civoid kunit_binary_str_assert_format(const struct kunit_assert *assert,
1338c2ecf20Sopenharmony_ci				    struct string_stream *stream)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	struct kunit_binary_str_assert *binary_assert = container_of(
1368c2ecf20Sopenharmony_ci			assert, struct kunit_binary_str_assert, assert);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	kunit_base_assert_format(assert, stream);
1398c2ecf20Sopenharmony_ci	string_stream_add(stream,
1408c2ecf20Sopenharmony_ci			  KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n",
1418c2ecf20Sopenharmony_ci			  binary_assert->left_text,
1428c2ecf20Sopenharmony_ci			  binary_assert->operation,
1438c2ecf20Sopenharmony_ci			  binary_assert->right_text);
1448c2ecf20Sopenharmony_ci	string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %s\n",
1458c2ecf20Sopenharmony_ci			  binary_assert->left_text,
1468c2ecf20Sopenharmony_ci			  binary_assert->left_value);
1478c2ecf20Sopenharmony_ci	string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %s",
1488c2ecf20Sopenharmony_ci			  binary_assert->right_text,
1498c2ecf20Sopenharmony_ci			  binary_assert->right_value);
1508c2ecf20Sopenharmony_ci	kunit_assert_print_msg(assert, stream);
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);
153