xref: /freebsd/lib/libc/tests/string/memcmp_test.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * Copyright (c) 2016 Jilles Tjoelker <jilles@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #include <assert.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include <atf-c.h>
34 
35 ATF_TC_WITHOUT_HEAD(zero);
36 ATF_TC_BODY(zero, tc)
37 {
38 
39 	assert(memcmp("a", "b", 0) == 0);
40 	assert(memcmp("", "", 0) == 0);
41 }
42 
43 ATF_TC_WITHOUT_HEAD(eq);
44 ATF_TC_BODY(eq, tc)
45 {
46 	unsigned char data1[256], data2[256];
47 	int i;
48 
49 	for (i = 0; i < 256; i++)
50 		data1[i] = data2[i] = i ^ 0x55;
51 	for (i = 1; i < 256; i++)
52 		assert(memcmp(data1, data2, i) == 0);
53 	for (i = 1; i < 256; i++)
54 		assert(memcmp(data1 + i, data2 + i, 256 - i) == 0);
55 }
56 
57 ATF_TC_WITHOUT_HEAD(neq);
58 ATF_TC_BODY(neq, tc)
59 {
60 	unsigned char data1[256], data2[256];
61 	int i;
62 
63 	for (i = 0; i < 256; i++) {
64 		data1[i] = i;
65 		data2[i] = i ^ 0x55;
66 	}
67 	for (i = 1; i < 256; i++)
68 		assert(memcmp(data1, data2, i) != 0);
69 	for (i = 1; i < 256; i++)
70 		assert(memcmp(data1 + i, data2 + i, 256 - i) != 0);
71 }
72 
73 ATF_TC_WITHOUT_HEAD(diff);
74 ATF_TC_BODY(diff, tc)
75 {
76 	unsigned char data1[256], data2[256];
77 	int i;
78 
79 	memset(data1, 'a', sizeof(data1));
80 	memset(data2, 'a', sizeof(data2));
81 	data1[128] = 255;
82 	data2[128] = 0;
83 	for (i = 1; i < 66; i++) {
84 		assert(memcmp(data1 + 128, data2 + 128, i) == 255);
85 		assert(memcmp(data2 + 128, data1 + 128, i) == -255);
86 		assert(memcmp(data1 + 129 - i, data2 + 129 - i, i) == 255);
87 		assert(memcmp(data2 + 129 - i, data1 + 129 - i, i) == -255);
88 		assert(memcmp(data1 + 129 - i, data2 + 129 - i, i * 2) == 255);
89 		assert(memcmp(data2 + 129 - i, data1 + 129 - i, i * 2) == -255);
90 	}
91 	data1[128] = 'c';
92 	data2[128] = 'e';
93 	for (i = 1; i < 66; i++) {
94 		assert(memcmp(data1 + 128, data2 + 128, i) == -2);
95 		assert(memcmp(data2 + 128, data1 + 128, i) == 2);
96 		assert(memcmp(data1 + 129 - i, data2 + 129 - i, i) == -2);
97 		assert(memcmp(data2 + 129 - i, data1 + 129 - i, i) == 2);
98 		assert(memcmp(data1 + 129 - i, data2 + 129 - i, i * 2) == -2);
99 		assert(memcmp(data2 + 129 - i, data1 + 129 - i, i * 2) == 2);
100 	}
101 	memset(data1 + 129, 'A', sizeof(data1) - 129);
102 	memset(data2 + 129, 'Z', sizeof(data2) - 129);
103 	for (i = 1; i < 66; i++) {
104 		assert(memcmp(data1 + 128, data2 + 128, i) == -2);
105 		assert(memcmp(data2 + 128, data1 + 128, i) == 2);
106 		assert(memcmp(data1 + 129 - i, data2 + 129 - i, i) == -2);
107 		assert(memcmp(data2 + 129 - i, data1 + 129 - i, i) == 2);
108 		assert(memcmp(data1 + 129 - i, data2 + 129 - i, i * 2) == -2);
109 		assert(memcmp(data2 + 129 - i, data1 + 129 - i, i * 2) == 2);
110 	}
111 }
112 
113 ATF_TP_ADD_TCS(tp)
114 {
115 
116 	ATF_TP_ADD_TC(tp, zero);
117 	ATF_TP_ADD_TC(tp, eq);
118 	ATF_TP_ADD_TC(tp, neq);
119 	ATF_TP_ADD_TC(tp, diff);
120 
121 	return (atf_no_error());
122 }
123