1 /*
2  * Copyright (c) 2016, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *  * Redistributions of source code must retain the above copyright notice,
8  *    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 the
11  *    documentation and/or other materials provided with the distribution.
12  *  * Neither the name of Intel Corporation nor the names of its contributors
13  *    may be used to endorse or promote products derived from this software
14  *    without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "config.h"
30 
31 #include "rose/validate_mask.h"
32 #include "gtest/gtest.h"
33 
34 #define ONES32 0xffffffffu
35 
36 union RoseLookaroundMask32 {
37     m256 a256;
38     u8 a8[32];
39 };
40 
41 struct ValidateMask32TestInfo {
42     RoseLookaroundMask32 data;
43     u32 valid_mask;
44     RoseLookaroundMask32 and_mask;
45     RoseLookaroundMask32 cmp_mask;
46     u32 neg_mask;
47 };
48 
49 struct ValidateMask32InitInfo {
50     int idx;
51     u8 data;
52     u8 and_mask;
53     u8 cmp_mask;
54     u8 neg_mask;
55 };
56 
57 
58 static const ValidateMask32InitInfo testBasicIdx[][33] = {
59     {
60         {1, 0x34, 0xf8, 0x30, 0},
61         {2, 0x34, 0xf8, 0x30, 0},
62         {8, 0x23, 0xff, 0x23, 0},
63         {9, 0x34, 0xf8, 0x30, 0},
64         {10, 0x41, 0xdf, 0x41, 0},
65         {11, 0x63, 0xdd, 0x41, 0},
66         {12, 0x61, 0xdd, 0x41, 0},
67         {13, 0x41, 0xdf, 0x41, 0},
68         {14, 0x61, 0xdf, 0x41, 0},
69         {15, 0x41, 0xdf, 0x41, 0},
70         {16, 0x43, 0xdd, 0x41, 0},
71         {17, 0x61, 0xdd, 0x41, 0},
72         {23, 0x63, 0xdd, 0x41, 0},
73         {24, 0x4f, 0xfc, 0x4c, 0},
74         {25, 0x4d, 0xfc, 0x4c, 0},
75         {26, 0x4d, 0xfc, 0x4c, 0},
76         {-1, 0, 0, 0, 0},
77     },
78     {
79         {11, 0, 0xff, 0x55, 1},
80         {12, 0, 0xff, 0x36, 1},
81         {13, 0, 0xfe, 0x34, 1},
82         {14, 0x4d, 0xfe, 0x4c, 0},
83         {15, 0x41, 0xbf, 0x01, 0},
84         {16, 0x53, 0xdf, 0x73, 1},
85         {17, 0x4b, 0, 0, 0},
86         {18, 0, 0x2c, 0x2c, 1},
87         {-1, 0, 0, 0, 0},
88     },
89     {
90         {15, 0x46, 0xdf, 0x46, 0},
91         {16, 0x4f, 0xdf, 0x46, 1},
92         {17, 0x6f, 0xff, 0x6f, 0},
93         {18, 0x31, 0xfe, 0x30, 0},
94         {19, 0x34, 0xf8, 0x30, 0},
95         {20, 0x66, 0xc0, 0x40, 0},
96         {21, 0x6f, 0xf0, 0x60, 0},
97         {22, 0x6f, 0, 0, 0},
98         {23, 0x46, 0xdf, 0x44, 1},
99         {24, 0x4f, 0xdf, 0x46, 1},
100         {25, 0x6f, 0xff, 0x4f, 1},
101         {26, 0x31, 0xfe, 0x30, 0},
102         {27, 0x34, 0xf8, 0x34, 1},
103         {28, 0x66, 0xc0, 0x60, 1},
104         {29, 0x6f, 0xf0, 0x6f, 1},
105         {30, 0x6f, 0, 0x60, 1},
106         {-1, 0, 0, 0, 0},
107     },
108     {
109         {31, 0x4a, 0x80, 0, 0},
110         {-1, 0, 0, 0, 1},
111     },
112     {
113         {12, 0x2b, 0x3d, 0x2d, 1},
114         {13, 0x2b, 0x3d, 0x4c, 1},
115         {23, 0x4a, 0x88, 0x0a, 1},
116         {-1, 0, 0, 0, 0},
117     },
118 };
119 
initTestInfo(ValidateMask32TestInfo & t)120 static void initTestInfo(ValidateMask32TestInfo &t) {
121     t.data.a256 = zeroes256();
122     t.valid_mask = 0xffffffff;
123     t.and_mask.a256 = zeroes256();
124     t.cmp_mask.a256 = zeroes256();
125     t.neg_mask = 0;
126 };
127 
128 
129 static
testBasicInit(ValidateMask32TestInfo * testB)130 int testBasicInit(ValidateMask32TestInfo *testB) {
131     int len = 0;
132     ValidateMask32TestInfo t;
133     for (size_t i = 0; i < ARRAY_LENGTH(testBasicIdx); i++) {
134         initTestInfo(t);
135         for (const auto &line: testBasicIdx[i]) {
136             if (line.idx < 0) {
137                 break;
138             }
139             int index = line.idx;
140             t.data.a8[index] = line.data;
141             t.and_mask.a8[index] = line.and_mask;
142             t.cmp_mask.a8[index] = line.cmp_mask;
143             t.neg_mask |= line.neg_mask << index;
144         }
145         testB[i] = t;
146         len++;
147     }
148     return len;
149 }
150 
TEST(ValidateMask32,testMask32_1)151 TEST(ValidateMask32, testMask32_1) {
152     ValidateMask32TestInfo testBasic[20];
153     int test_len = testBasicInit(testBasic);
154     for (int i = 0; i < test_len; i++) {
155         const auto t = testBasic[i];
156         EXPECT_EQ(1, validateMask32(t.data.a256, t.valid_mask,
157                                     t.and_mask.a256, t.cmp_mask.a256,
158                                     t.neg_mask));
159     }
160 }
161 
TEST(ValidateMask32,testMask32_2)162 TEST(ValidateMask32, testMask32_2) {
163     ValidateMask32TestInfo testBasic[20];
164     int test_len = testBasicInit(testBasic);
165     for (int left = 0; left <= 32; left++) {
166         for (int right = 0; right + left < 32; right++) {
167             u32 valid_mask = ONES32 << (left + right) >> left;
168             for (int i = 0; i < test_len; i++) {
169                 const auto &t = testBasic[i];
170                 int bool_result;
171                 bool_result = !(valid_mask & t.neg_mask);
172                 EXPECT_EQ(bool_result, validateMask32(t.data.a256,
173                                                       valid_mask,
174                                                       t.and_mask.a256,
175                                                       t.cmp_mask.a256,
176                                                       0));
177                 bool_result = (valid_mask & t.neg_mask) == valid_mask;
178                 EXPECT_EQ(bool_result, validateMask32(t.data.a256,
179                                                       valid_mask,
180                                                       t.and_mask.a256,
181                                                       t.cmp_mask.a256,
182                                                       ONES32));
183             }
184         }
185     }
186 }
187 
TEST(ValidateMask32,testMask32_3)188 TEST(ValidateMask32, testMask32_3) {
189     ValidateMask32TestInfo testBasic[20];
190     testing::internal::Random neg_mask_rand(451);
191     int test_len = testBasicInit(testBasic);
192     for (int left = 0; left <= 32; left++) {
193         for (int right = 0; right + left < 32; right++) {
194             u32 valid_mask = ONES32 << (left + right) >> left;
195             for (int i = 0; i < test_len; i++) {
196                 const auto &t = testBasic[i];
197                 int bool_result;
198                 for (int j = 0; j < 5000; j++) {
199                     u32 neg_mask = neg_mask_rand.Generate(1u << 31);
200                     bool_result = (neg_mask & valid_mask) ==
201                                   (t.neg_mask & valid_mask);
202                     EXPECT_EQ(bool_result, validateMask32(t.data.a256,
203                                                           valid_mask,
204                                                           t.and_mask.a256,
205                                                           t.cmp_mask.a256,
206                                                           neg_mask));
207                 }
208             }
209         }
210     }
211 }
212