Skip to content

Commit b830135

Browse files
committed
lib: Add TST_ASSERT_INT() and TST_ASSERT_STR()
These functions are meant for asserting that a sysfs or a procfs file holds the correct value, which is much faster than reading the file, comapring the value and issuing PASS/FAIL message. Signed-off-by: Cyril Hrubis <[email protected]> Signed-off-by: Yang Xu <[email protected]>
1 parent 5c7c43d commit b830135

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

include/tst_assert.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
4+
* Author: Yang Xu <[email protected]>
5+
* Copyright (c) 2020 Cyril Hrubis <[email protected]>
6+
*/
7+
#ifndef TST_ASSERT_H__
8+
#define TST_ASSERT_H__
9+
10+
#define TST_ASSERT_INT(path, val) \
11+
tst_assert_int(__FILE__, __LINE__, path, val)
12+
13+
/*
14+
* Asserts that integer value stored in file pointed by path equals to the
15+
* value passed to this function. This is mostly useful for asserting correct
16+
* values in sysfs, procfs, etc.
17+
*/
18+
void tst_assert_int(const char *file, const int lineno,
19+
const char *path, int val);
20+
21+
#define TST_ASSERT_STR(path, val) \
22+
tst_assert_str(__FILE__, __LINE__, path, val)
23+
24+
/*
25+
* Asserts that a string value stored in file pointed by path equals to the
26+
* value passed to this function. This is mostly useful for asserting correct
27+
* values in sysfs, procfs, etc.
28+
*/
29+
void tst_assert_str(const char *file, const int lineno,
30+
const char *path, const char *val);
31+
32+
#endif /* TST_ASSERT_H__ */

include/tst_test.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "tst_buffers.h"
3939
#include "tst_capability.h"
4040
#include "tst_hugepage.h"
41+
#include "tst_assert.h"
4142

4243
/*
4344
* Reports testcase result.

lib/tst_assert.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
4+
* Author: Yang Xu <[email protected]>
5+
* Copyright (c) 2020 Cyril Hrubis <[email protected]>
6+
*/
7+
#define TST_NO_DEFAULT_MAIN
8+
#include "tst_assert.h"
9+
#include "tst_test.h"
10+
11+
void tst_assert_int(const char *file, const int lineno, const char *path, int val)
12+
{
13+
int sys_val;
14+
15+
SAFE_FILE_SCANF(path, "%d", &sys_val);
16+
17+
if (val == sys_val) {
18+
tst_res_(file, lineno, TPASS, "%s = %d", path, val);
19+
return;
20+
}
21+
22+
tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, sys_val);
23+
}
24+
25+
void tst_assert_str(const char *file, const int lineno, const char *path, const char *val)
26+
{
27+
char sys_val[1024];
28+
29+
SAFE_FILE_SCANF(path, "%1024s", sys_val);
30+
if (!strcmp(val, sys_val)) {
31+
tst_res_(file, lineno, TPASS, "%s = '%s'", path, val);
32+
return;
33+
}
34+
35+
tst_res_(file, lineno, TFAIL, "%s != '%s' got '%s'", path, val, sys_val);
36+
}

0 commit comments

Comments
 (0)