1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
4 * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
5 * Copyright (c) 2020 Cyril Hrubis <chrubis@suse.cz>
6 */
7 #include <stdio.h>
8 #define TST_NO_DEFAULT_MAIN
9 #include "tst_assert.h"
10 #include "tst_test.h"
11
tst_assert_int(const char * file,const int lineno,const char * path,int val)12 void tst_assert_int(const char *file, const int lineno, const char *path, int val)
13 {
14 int sys_val;
15
16 safe_file_scanf(file, lineno, NULL, path, "%d", &sys_val);
17
18 if (val == sys_val) {
19 tst_res_(file, lineno, TPASS, "%s = %d", path, val);
20 return;
21 }
22
23 tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, sys_val);
24 }
25
tst_assert_ulong(const char * file,const int lineno,const char * path,unsigned long val)26 void tst_assert_ulong(const char *file, const int lineno, const char *path, unsigned long val)
27 {
28 unsigned long sys_val;
29
30 safe_file_scanf(file, lineno, NULL, path, "%lu", &sys_val);
31
32 if (val == sys_val) {
33 tst_res_(file, lineno, TPASS, "%s = %lu", path, val);
34 return;
35 }
36
37 tst_res_(file, lineno, TFAIL, "%s != %lu got %lu", path, val, sys_val);
38 }
39
tst_assert_file_int(const char * file,const int lineno,const char * path,const char * prefix,int val)40 void tst_assert_file_int(const char *file, const int lineno, const char *path, const char *prefix, int val)
41 {
42 int sys_val;
43 char fmt[1024];
44
45 snprintf(fmt, sizeof(fmt), "%s%%d", prefix);
46 file_lines_scanf(file, lineno, NULL, 1, path, fmt, &sys_val);
47
48 if (val == sys_val) {
49 tst_res_(file, lineno, TPASS, "%s %s = %d", path, prefix, sys_val);
50 return;
51 }
52
53 tst_res_(file, lineno, TFAIL, "%s %s != %d got %d", path, prefix, val, sys_val);
54 }
55
tst_assert_str(const char * file,const int lineno,const char * path,const char * val)56 void tst_assert_str(const char *file, const int lineno, const char *path, const char *val)
57 {
58 char sys_val[1024];
59
60 safe_file_scanf(file, lineno, NULL, path, "%1023s", sys_val);
61 if (!strcmp(val, sys_val)) {
62 tst_res_(file, lineno, TPASS, "%s = '%s'", path, val);
63 return;
64 }
65
66 tst_res_(file, lineno, TFAIL, "%s != '%s' got '%s'", path, val, sys_val);
67 }
68
tst_assert_file_str(const char * file,const int lineno,const char * path,const char * prefix,const char * val)69 void tst_assert_file_str(const char *file, const int lineno, const char *path, const char *prefix, const char *val)
70 {
71 char sys_val[1024];
72 char fmt[2048];
73
74 snprintf(fmt, sizeof(fmt), "%s: %%1023s", prefix);
75 file_lines_scanf(file, lineno, NULL, 1, path, fmt, sys_val);
76
77 if (!strcmp(val, sys_val)) {
78 tst_res_(file, lineno, TPASS, "%s %s = '%s'", path, prefix, sys_val);
79 return;
80 }
81
82 tst_res_(file, lineno, TFAIL, "%s %s != '%s' got '%s'", path, prefix, val, sys_val);
83 }
84