1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "memcmp16.h"
#include "gtest/gtest.h"
class RandGen {
public:
explicit RandGen(uint32_t seed) : val_(seed) {}
uint32_t next() {
val_ = val_ * 48271 % 2147483647 + 13;
return val_;
}
uint32_t val_;
};
class MemCmp16Test : public testing::Test {
};
// A simple implementation to compare against.
// Note: this version is equivalent to the generic one used when no optimized version is available.
int32_t memcmp16_compare(const uint16_t* s0, const uint16_t* s1, size_t count) {
for (size_t i = 0; i < count; i++) {
if (s0[i] != s1[i]) {
return static_cast<int32_t>(s0[i]) - static_cast<int32_t>(s1[i]);
}
}
return 0;
}
static constexpr size_t kMemCmp16Rounds = 100000;
static void CheckSeparate(size_t max_length, size_t min_length) {
RandGen r(0x1234);
size_t range_of_tests = 7; // All four (weighted) tests active in the beginning.
for (size_t round = 0; round < kMemCmp16Rounds; ++round) {
size_t type = r.next() % range_of_tests;
size_t count1, count2;
uint16_t *s1, *s2; // Use raw pointers to simplify using clobbered addresses
switch (type) {
case 0: // random, non-zero lengths of both strings
case 1:
case 2:
case 3:
count1 = (r.next() % max_length) + min_length;
count2 = (r.next() % max_length) + min_length;
break;
case 4: // random non-zero length of first, second is zero
count1 = (r.next() % max_length) + min_length;
count2 = 0U;
break;
case 5: // random non-zero length of second, first is zero
count1 = 0U;
count2 = (r.next() % max_length) + min_length;
break;
case 6: // both zero-length
count1 = 0U;
count2 = 0U;
range_of_tests = 6; // Don't do zero-zero again.
break;
default:
ASSERT_TRUE(false) << "Should not get here.";
continue;
}
if (count1 > 0U) {
s1 = new uint16_t[count1];
} else {
// Leave a random pointer, should not be touched.
s1 = reinterpret_cast<uint16_t*>(0xebad1001);
}
if (count2 > 0U) {
s2 = new uint16_t[count2];
} else {
// Leave a random pointer, should not be touched.
s2 = reinterpret_cast<uint16_t*>(0xebad2002);
}
size_t min = count1 < count2 ? count1 : count2;
bool fill_same = r.next() % 2 == 1;
if (fill_same) {
for (size_t i = 0; i < min; ++i) {
s1[i] = static_cast<uint16_t>(r.next() & 0xFFFF);
s2[i] = s1[i];
}
for (size_t i = min; i < count1; ++i) {
s1[i] = static_cast<uint16_t>(r.next() & 0xFFFF);
}
for (size_t i = min; i < count2; ++i) {
s2[i] = static_cast<uint16_t>(r.next() & 0xFFFF);
}
} else {
for (size_t i = 0; i < count1; ++i) {
s1[i] = static_cast<uint16_t>(r.next() & 0xFFFF);
}
for (size_t i = 0; i < count2; ++i) {
s2[i] = static_cast<uint16_t>(r.next() & 0xFFFF);
}
}
uint16_t* s1_pot_unaligned = s1;
uint16_t* s2_pot_unaligned = s2;
size_t c1_mod = count1;
size_t c2_mod = count2;
if (!fill_same) { // Don't waste a good "long" test.
if (count1 > 1 && r.next() % 10 == 0) {
c1_mod--;
s1_pot_unaligned++;
}
if (count2 > 1 && r.next() % 10 == 0) {
c2_mod--;
s2_pot_unaligned++;
}
}
size_t mod_min = c1_mod < c2_mod ? c1_mod : c2_mod;
int32_t expected = memcmp16_compare(s1_pot_unaligned, s2_pot_unaligned, mod_min);
int32_t computed = art::testing::MemCmp16Testing(s1_pot_unaligned, s2_pot_unaligned, mod_min);
ASSERT_EQ(expected, computed) << "Run " << round << ", c1=" << count1 << " c2=" << count2;
if (count1 > 0U) {
delete[] s1;
}
if (count2 > 0U) {
delete[] s2;
}
}
}
TEST_F(MemCmp16Test, RandomSeparateShort) {
CheckSeparate(5U, 1U);
}
TEST_F(MemCmp16Test, RandomSeparateLong) {
CheckSeparate(64U, 32U);
}
// TODO: What's a good test for overlapping memory. Is it important?
// TEST_F(MemCmp16Test, RandomOverlay) {
//
// }
|