1 /* test_compare256.cc -- compare256 unit tests
2 * Copyright (C) 2022 Nathan Moinvaziri
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9
10 extern "C" {
11 # include "zbuild.h"
12 # include "zutil_p.h"
13 # include "cpu_features.h"
14 }
15
16 #include <gtest/gtest.h>
17
18 #define MAX_COMPARE_SIZE (256)
19
20 /* Ensure that compare256 returns the correct match length */
compare256_match_check(compare256_func compare256)21 static inline void compare256_match_check(compare256_func compare256) {
22 int32_t match_len, i;
23 uint8_t *str1;
24 uint8_t *str2;
25
26 str1 = (uint8_t *)zng_alloc(MAX_COMPARE_SIZE);
27 ASSERT_TRUE(str1 != NULL);
28 memset(str1, 'a', MAX_COMPARE_SIZE);
29
30 str2 = (uint8_t *)zng_alloc(MAX_COMPARE_SIZE);
31 ASSERT_TRUE(str2 != NULL);
32 memset(str2, 'a', MAX_COMPARE_SIZE);
33
34 for (i = 0; i <= MAX_COMPARE_SIZE; i++) {
35 if (i < MAX_COMPARE_SIZE)
36 str2[i] = 0;
37
38 match_len = compare256(str1, str2);
39 EXPECT_EQ(match_len, i);
40
41 if (i < MAX_COMPARE_SIZE)
42 str2[i] = 'a';
43 }
44
45 zng_free(str1);
46 zng_free(str2);
47 }
48
49 #define TEST_COMPARE256(name, func, support_flag) \
50 TEST(compare256, name) { \
51 if (!support_flag) { \
52 GTEST_SKIP(); \
53 return; \
54 } \
55 compare256_match_check(func); \
56 }
57
58 TEST_COMPARE256(c, compare256_c, 1)
59
60 #ifdef UNALIGNED_OK
61 TEST_COMPARE256(unaligned_16, compare256_unaligned_16, 1)
62 #ifdef HAVE_BUILTIN_CTZ
63 TEST_COMPARE256(unaligned_32, compare256_unaligned_32, 1)
64 #endif
65 #if defined(UNALIGNED64_OK) && defined(HAVE_BUILTIN_CTZLL)
66 TEST_COMPARE256(unaligned_64, compare256_unaligned_64, 1)
67 #endif
68 #endif
69 #if defined(X86_SSE2) && defined(HAVE_BUILTIN_CTZ)
70 TEST_COMPARE256(sse2, compare256_sse2, x86_cpu_has_sse2)
71 #endif
72 #if defined(X86_AVX2) && defined(HAVE_BUILTIN_CTZ)
73 TEST_COMPARE256(avx2, compare256_avx2, x86_cpu_has_avx2)
74 #endif
75 #if defined(ARM_NEON) && defined(HAVE_BUILTIN_CTZLL)
76 TEST_COMPARE256(neon, compare256_neon, arm_cpu_has_neon)
77 #endif
78 #ifdef POWER9
79 TEST_COMPARE256(power9, compare256_power9, power_cpu_has_arch_3_00)
80 #endif
81