xref: /freebsd/lib/libc/tests/string/memcmp_test.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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 __FBSDID("$FreeBSD$");
29 
30 #include <assert.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 #include <atf-c.h>
36 
37 ATF_TC_WITHOUT_HEAD(zero);
38 ATF_TC_BODY(zero, tc)
39 {
40 
41 	assert(memcmp("a", "b", 0) == 0);
42 	assert(memcmp("", "", 0) == 0);
43 }
44 
45 ATF_TC_WITHOUT_HEAD(eq);
46 ATF_TC_BODY(eq, tc)
47 {
48 	unsigned char data1[256], data2[256];
49 	int i;
50 
51 	for (i = 0; i < 256; i++)
52 		data1[i] = data2[i] = i ^ 0x55;
53 	for (i = 1; i < 256; i++)
54 		assert(memcmp(data1, data2, i) == 0);
55 	for (i = 1; i < 256; i++)
56 		assert(memcmp(data1 + i, data2 + i, 256 - i) == 0);
57 }
58 
59 ATF_TC_WITHOUT_HEAD(neq);
60 ATF_TC_BODY(neq, tc)
61 {
62 	unsigned char data1[256], data2[256];
63 	int i;
64 
65 	for (i = 0; i < 256; i++) {
66 		data1[i] = i;
67 		data2[i] = i ^ 0x55;
68 	}
69 	for (i = 1; i < 256; i++)
70 		assert(memcmp(data1, data2, i) != 0);
71 	for (i = 1; i < 256; i++)
72 		assert(memcmp(data1 + i, data2 + i, 256 - i) != 0);
73 }
74 
75 ATF_TC_WITHOUT_HEAD(diff);
76 ATF_TC_BODY(diff, tc)
77 {
78 	unsigned char data1[256], data2[256];
79 	int i;
80 
81 	memset(data1, 'a', sizeof(data1));
82 	memset(data2, 'a', sizeof(data2));
83 	data1[128] = 255;
84 	data2[128] = 0;
85 	for (i = 1; i < 66; i++) {
86 		assert(memcmp(data1 + 128, data2 + 128, i) == 255);
87 		assert(memcmp(data2 + 128, data1 + 128, i) == -255);
88 		assert(memcmp(data1 + 129 - i, data2 + 129 - i, i) == 255);
89 		assert(memcmp(data2 + 129 - i, data1 + 129 - i, i) == -255);
90 		assert(memcmp(data1 + 129 - i, data2 + 129 - i, i * 2) == 255);
91 		assert(memcmp(data2 + 129 - i, data1 + 129 - i, i * 2) == -255);
92 	}
93 	data1[128] = 'c';
94 	data2[128] = 'e';
95 	for (i = 1; i < 66; i++) {
96 		assert(memcmp(data1 + 128, data2 + 128, i) == -2);
97 		assert(memcmp(data2 + 128, data1 + 128, i) == 2);
98 		assert(memcmp(data1 + 129 - i, data2 + 129 - i, i) == -2);
99 		assert(memcmp(data2 + 129 - i, data1 + 129 - i, i) == 2);
100 		assert(memcmp(data1 + 129 - i, data2 + 129 - i, i * 2) == -2);
101 		assert(memcmp(data2 + 129 - i, data1 + 129 - i, i * 2) == 2);
102 	}
103 	memset(data1 + 129, 'A', sizeof(data1) - 129);
104 	memset(data2 + 129, 'Z', sizeof(data2) - 129);
105 	for (i = 1; i < 66; i++) {
106 		assert(memcmp(data1 + 128, data2 + 128, i) == -2);
107 		assert(memcmp(data2 + 128, data1 + 128, i) == 2);
108 		assert(memcmp(data1 + 129 - i, data2 + 129 - i, i) == -2);
109 		assert(memcmp(data2 + 129 - i, data1 + 129 - i, i) == 2);
110 		assert(memcmp(data1 + 129 - i, data2 + 129 - i, i * 2) == -2);
111 		assert(memcmp(data2 + 129 - i, data1 + 129 - i, i * 2) == 2);
112 	}
113 }
114 
115 ATF_TP_ADD_TCS(tp)
116 {
117 
118 	ATF_TP_ADD_TC(tp, zero);
119 	ATF_TP_ADD_TC(tp, eq);
120 	ATF_TP_ADD_TC(tp, neq);
121 	ATF_TP_ADD_TC(tp, diff);
122 
123 	return (atf_no_error());
124 }
125