Merge remote-tracking branches 'asoc/topic/ac97', 'asoc/topic/ac97-mfd', 'asoc/topic...
[sfrench/cifs-2.6.git] / tools / testing / selftests / powerpc / stringloops / memcmp.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <malloc.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "utils.h"
6
7 #define SIZE 256
8 #define ITERATIONS 10000
9
10 int test_memcmp(const void *s1, const void *s2, size_t n);
11
12 /* test all offsets and lengths */
13 static void test_one(char *s1, char *s2)
14 {
15         unsigned long offset, size;
16
17         for (offset = 0; offset < SIZE; offset++) {
18                 for (size = 0; size < (SIZE-offset); size++) {
19                         int x, y;
20                         unsigned long i;
21
22                         y = memcmp(s1+offset, s2+offset, size);
23                         x = test_memcmp(s1+offset, s2+offset, size);
24
25                         if (((x ^ y) < 0) &&    /* Trick to compare sign */
26                                 ((x | y) != 0)) { /* check for zero */
27                                 printf("memcmp returned %d, should have returned %d (offset %ld size %ld)\n", x, y, offset, size);
28
29                                 for (i = offset; i < offset+size; i++)
30                                         printf("%02x ", s1[i]);
31                                 printf("\n");
32
33                                 for (i = offset; i < offset+size; i++)
34                                         printf("%02x ", s2[i]);
35                                 printf("\n");
36                                 abort();
37                         }
38                 }
39         }
40 }
41
42 static int testcase(void)
43 {
44         char *s1;
45         char *s2;
46         unsigned long i;
47
48         s1 = memalign(128, SIZE);
49         if (!s1) {
50                 perror("memalign");
51                 exit(1);
52         }
53
54         s2 = memalign(128, SIZE);
55         if (!s2) {
56                 perror("memalign");
57                 exit(1);
58         }
59
60         srandom(1);
61
62         for (i = 0; i < ITERATIONS; i++) {
63                 unsigned long j;
64                 unsigned long change;
65
66                 for (j = 0; j < SIZE; j++)
67                         s1[j] = random();
68
69                 memcpy(s2, s1, SIZE);
70
71                 /* change one byte */
72                 change = random() % SIZE;
73                 s2[change] = random() & 0xff;
74
75                 test_one(s1, s2);
76         }
77
78         srandom(1);
79
80         for (i = 0; i < ITERATIONS; i++) {
81                 unsigned long j;
82                 unsigned long change;
83
84                 for (j = 0; j < SIZE; j++)
85                         s1[j] = random();
86
87                 memcpy(s2, s1, SIZE);
88
89                 /* change multiple bytes, 1/8 of total */
90                 for (j = 0; j < SIZE / 8; j++) {
91                         change = random() % SIZE;
92                         s2[change] = random() & 0xff;
93                 }
94
95                 test_one(s1, s2);
96         }
97
98         return 0;
99 }
100
101 int main(void)
102 {
103         return test_harness(testcase, "memcmp");
104 }