xref: /isa-l/erasure_code/gf_vect_mul_base_test.c (revision 9ab5a9e579c4fb4e2a3c92d73ccd6d97291d0e80)
1 /**********************************************************************
2   Copyright(c) 2011-2015 Intel Corporation All rights reserved.
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
6   are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in
11       the documentation and/or other materials provided with the
12       distribution.
13     * Neither the name of Intel Corporation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h> // for memset
33 #include "erasure_code.h"
34 
35 #define TEST_SIZE     8192
36 #define TEST_MEM      TEST_SIZE
37 #define TEST_LOOPS    100000
38 #define TEST_TYPE_STR ""
39 
40 typedef unsigned char u8;
41 
42 int
43 main(int argc, char *argv[])
44 {
45         int i;
46         u8 *buff1, *buff2, *buff3, gf_const_tbl[64], a = 2;
47         int align, size;
48         unsigned char *efence_buff1;
49         unsigned char *efence_buff2;
50 
51         printf("gf_vect_mul_base_test:\n");
52 
53         gf_vect_mul_init(a, gf_const_tbl);
54 
55         buff1 = (u8 *) malloc(TEST_SIZE);
56         buff2 = (u8 *) malloc(TEST_SIZE);
57         buff3 = (u8 *) malloc(TEST_SIZE);
58 
59         if (NULL == buff1 || NULL == buff2 || NULL == buff3) {
60                 printf("buffer alloc error\n");
61                 return -1;
62         }
63         // Fill with rand data
64         for (i = 0; i < TEST_SIZE; i++)
65                 buff1[i] = rand();
66 
67         if (gf_vect_mul_base(TEST_SIZE, gf_const_tbl, buff1, buff2) != 0) {
68                 printf("fail fill with rand data\n");
69                 return 1;
70         }
71 
72         for (i = 0; i < TEST_SIZE; i++)
73                 if (gf_mul(a, buff1[i]) != buff2[i]) {
74                         printf("fail at %d, 0x%x x 2 = 0x%x (0x%x)\n", i, buff1[i], buff2[i],
75                                gf_mul(2, buff1[i]));
76                         return 1;
77                 }
78 
79         if (gf_vect_mul_base(TEST_SIZE, gf_const_tbl, buff1, buff3) != 0) {
80                 printf("fail fill with rand data for buff1\n");
81                 return -1;
82         }
83         // Check reference function
84         for (i = 0; i < TEST_SIZE; i++)
85                 if (buff2[i] != buff3[i]) {
86                         printf("fail at %d, 0x%x x 0x%d = 0x%x (0x%x)\n", i, a, buff1[i], buff2[i],
87                                gf_mul(a, buff1[i]));
88                         return 1;
89                 }
90 
91         for (i = 0; i < TEST_SIZE; i++)
92                 buff1[i] = rand();
93 
94         // Check each possible constant
95         printf("Random tests ");
96         for (a = 0; a != 255; a++) {
97                 gf_vect_mul_init(a, gf_const_tbl);
98                 if (gf_vect_mul_base(TEST_SIZE, gf_const_tbl, buff1, buff2) != 0) {
99                         printf("fail random tests\n");
100                         return 1;
101                 }
102 
103                 for (i = 0; i < TEST_SIZE; i++)
104                         if (gf_mul(a, buff1[i]) != buff2[i]) {
105                                 printf("fail at %d, 0x%x x %d = 0x%x (0x%x)\n", i, a, buff1[i],
106                                        buff2[i], gf_mul(2, buff1[i]));
107                                 return 1;
108                         }
109 #ifdef TEST_VERBOSE
110                 putchar('.');
111 #endif
112         }
113 
114         // Run tests at end of buffer for Electric Fence
115         align = 32;
116         a = 2;
117 
118         gf_vect_mul_init(a, gf_const_tbl);
119         for (size = 0; size < TEST_SIZE; size += align) {
120                 // Line up TEST_SIZE from end
121                 efence_buff1 = buff1 + size;
122                 efence_buff2 = buff2 + size;
123 
124                 if (gf_vect_mul_base(TEST_SIZE - size, gf_const_tbl, efence_buff1, efence_buff2) !=
125                     0) {
126                         printf("fail tests at end of buffer\n");
127                         return -1;
128                 }
129 
130                 for (i = 0; i < TEST_SIZE - size; i++)
131                         if (gf_mul(a, efence_buff1[i]) != efence_buff2[i]) {
132                                 printf("fail at %d, 0x%x x 2 = 0x%x (0x%x)\n", i, efence_buff1[i],
133                                        efence_buff2[i], gf_mul(2, efence_buff1[i]));
134                                 return 1;
135                         }
136 
137 #ifdef TEST_VERBOSE
138                 putchar('.');
139 #endif
140         }
141 
142         printf(" done: Pass\n");
143         return 0;
144 }
145