소스 검색

[test] Add self-tests for string functions

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 년 전
부모
커밋
520323e360
2개의 변경된 파일134개의 추가작업 그리고 0개의 파일을 삭제
  1. 133
    0
      src/tests/string_test.c
  2. 1
    0
      src/tests/tests.c

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

@@ -0,0 +1,133 @@
1
+/*
2
+ * Copyright (C) 2012 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 (at your option) 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
+ * String self-tests
25
+ *
26
+ * memcpy() tests are handled separately
27
+ */
28
+
29
+/* Forcibly enable assertions */
30
+#undef NDEBUG
31
+
32
+#include <stdint.h>
33
+#include <stdlib.h>
34
+#include <string.h>
35
+#include <ipxe/test.h>
36
+
37
+/**
38
+ * Perform string self-tests
39
+ *
40
+ */
41
+static void string_test_exec ( void ) {
42
+
43
+	/* Test strlen() */
44
+	ok ( strlen ( "" ) == 0 );
45
+	ok ( strlen ( "Hello" ) == 5 );
46
+	ok ( strlen ( "Hello world!" ) == 12 );
47
+	ok ( strlen ( "Hello\0world!" ) == 5 );
48
+
49
+	/* Test strnlen() */
50
+	ok ( strnlen ( "", 0 ) == 0 );
51
+	ok ( strnlen ( "", 10 ) == 0 );
52
+	ok ( strnlen ( "Hello", 0 ) == 0 );
53
+	ok ( strnlen ( "Hello", 3 ) == 3 );
54
+	ok ( strnlen ( "Hello", 5 ) == 5 );
55
+	ok ( strnlen ( "Hello", 16 ) == 5 );
56
+	ok ( strnlen ( "Hello world!", 5 ) == 5 );
57
+	ok ( strnlen ( "Hello world!", 11 ) == 11 );
58
+	ok ( strnlen ( "Hello world!", 16 ) == 12 );
59
+
60
+	/* Test strchr() */
61
+	ok ( strchr ( "", 'a' ) == NULL );
62
+	ok ( *(strchr ( "Testing", 'e' )) == 'e' );
63
+	ok ( *(strchr ( "Testing", 'g' )) == 'g' );
64
+	ok ( strchr ( "Testing", 'x' ) == NULL );
65
+
66
+	/* Test strcmp() */
67
+	ok ( strcmp ( "", "" ) == 0 );
68
+	ok ( strcmp ( "Hello", "Hello" ) == 0 );
69
+	ok ( strcmp ( "Hello", "hello" ) != 0 );
70
+	ok ( strcmp ( "Hello", "Hello world!" ) != 0 );
71
+	ok ( strcmp ( "Hello world!", "Hello" ) != 0 );
72
+
73
+	/* Test strncmp() */
74
+	ok ( strncmp ( "", "", 0 ) == 0 );
75
+	ok ( strncmp ( "", "", 15 ) == 0 );
76
+	ok ( strncmp ( "Goodbye", "Goodbye", 16 ) == 0 );
77
+	ok ( strncmp ( "Goodbye", "Hello", 16 ) != 0 );
78
+	ok ( strncmp ( "Goodbye", "Goodbye world", 32 ) != 0 );
79
+	ok ( strncmp ( "Goodbye", "Goodbye world", 7 ) == 0 );
80
+
81
+	/* Test memcmp() */
82
+	ok ( memcmp ( "", "", 0 ) == 0 );
83
+	ok ( memcmp ( "Foo", "Foo", 3 ) == 0 );
84
+	ok ( memcmp ( "Foo", "Bar", 3 ) != 0 );
85
+
86
+	/* Test memset() */
87
+	{
88
+		static uint8_t test[7] = { '>', 1, 1, 1, 1, 1, '<' };
89
+		static const uint8_t expected[7] = { '>', 0, 0, 0, 0, 0, '<' };
90
+		memset ( ( test + 1 ), 0, ( sizeof ( test ) - 2 ) );
91
+		ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
92
+	}
93
+	{
94
+		static uint8_t test[4] = { '>', 0, 0, '<' };
95
+		static const uint8_t expected[4] = { '>', 0xeb, 0xeb, '<' };
96
+		memset ( ( test + 1 ), 0xeb, ( sizeof ( test ) - 2 ) );
97
+		ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
98
+	}
99
+
100
+	/* Test memmove() */
101
+	{
102
+		static uint8_t test[11] =
103
+			{ '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, '<' };
104
+		static const uint8_t expected[11] =
105
+			{ '>', 3, 4, 5, 6, 7, 8, 7, 8, 9, '<' };
106
+		memmove ( ( test + 1 ), ( test + 3 ), 6 );
107
+		ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
108
+	}
109
+	{
110
+		static uint8_t test[12] =
111
+			{ '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '<' };
112
+		static const uint8_t expected[12] =
113
+			{ '>', 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, '<' };
114
+		memmove ( ( test + 6 ), ( test + 1 ), 5 );
115
+		ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
116
+	}
117
+
118
+	/* Test memswap() */
119
+	{
120
+		static uint8_t test[8] =
121
+			{ '>', 1, 2, 3, 7, 8, 9, '<' };
122
+		static const uint8_t expected[8] =
123
+			{ '>', 7, 8, 9, 1, 2, 3, '<' };
124
+		memswap ( ( test + 1 ), ( test + 4 ), 3 );
125
+		ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
126
+	}
127
+}
128
+
129
+/** String self-test */
130
+struct self_test string_test __self_test = {
131
+	.name = "string",
132
+	.exec = string_test_exec,
133
+};

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

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

Loading…
취소
저장