소스 검색

[test] Add self-tests for flsl()

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 10 년 전
부모
커밋
8f0e0e1356
2개의 변경된 파일87개의 추가작업 그리고 0개의 파일을 삭제
  1. 86
    0
      src/tests/math_test.c
  2. 1
    0
      src/tests/tests.c

+ 86
- 0
src/tests/math_test.c 파일 보기

@@ -0,0 +1,86 @@
1
+/*
2
+ * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17
+ * 02110-1301, USA.
18
+ */
19
+
20
+FILE_LICENCE ( GPL2_OR_LATER );
21
+
22
+/** @file
23
+ *
24
+ * Mathematical self-tests
25
+ *
26
+ */
27
+
28
+/* Forcibly enable assertions */
29
+#undef NDEBUG
30
+
31
+#include <string.h>
32
+#include <strings.h>
33
+#include <assert.h>
34
+#include <ipxe/test.h>
35
+
36
+/**
37
+ * Force a call to the non-constant implementation of flsl()
38
+ *
39
+ * @v value		Value
40
+ * @ret msb		Most significant bit set in value (LSB=1), or zero
41
+ */
42
+__attribute__ (( noinline )) int flsl_var ( long value ) {
43
+	return flsl ( value );
44
+}
45
+
46
+/**
47
+ * Report a flsl() test result
48
+ *
49
+ * @v value		Value
50
+ * @v msb		Expected MSB
51
+ * @v file		Test code file
52
+ * @v line		Test code line
53
+ */
54
+static inline __attribute__ (( always_inline )) void
55
+flsl_okx ( long value, int msb, const char *file, unsigned int line ) {
56
+
57
+	/* Verify as a constant (requires to be inlined) */
58
+	okx ( flsl ( value ) == msb, file, line );
59
+
60
+	/* Verify as a non-constant */
61
+	okx ( flsl_var ( value ) == msb, file, line );
62
+}
63
+#define flsl_ok( value, msb ) flsl_okx ( value, msb, __FILE__, __LINE__ )
64
+
65
+/**
66
+ * Perform mathematical self-tests
67
+ *
68
+ */
69
+static void math_test_exec ( void ) {
70
+
71
+	/* Test flsl() */
72
+	flsl_ok ( 0, 0 );
73
+	flsl_ok ( 1, 1 );
74
+	flsl_ok ( 255, 8 );
75
+	flsl_ok ( 256, 9 );
76
+	flsl_ok ( 257, 9 );
77
+	flsl_ok ( 0x69505845, 31 );
78
+	flsl_ok ( -1U, ( 8 * sizeof ( int ) ) );
79
+	flsl_ok ( -1UL, ( 8 * sizeof ( long ) ) );
80
+}
81
+
82
+/** Mathematical self-tests */
83
+struct self_test math_test __self_test = {
84
+	.name = "math",
85
+	.exec = math_test_exec,
86
+};

+ 1
- 0
src/tests/tests.c 파일 보기

@@ -28,6 +28,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
28 28
 /* Drag in all applicable self-tests */
29 29
 REQUIRE_OBJECT ( memcpy_test );
30 30
 REQUIRE_OBJECT ( string_test );
31
+REQUIRE_OBJECT ( math_test );
31 32
 REQUIRE_OBJECT ( vsprintf_test );
32 33
 REQUIRE_OBJECT ( list_test );
33 34
 REQUIRE_OBJECT ( byteswap_test );

Loading…
취소
저장