mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Fixed macro hash_vec(vec): it was checking only the first bytes (4 or 8, not sure) (#2718)
* Fixed macro hash_vec(vec): it was checking only 8 bytes * use $sizeof() directly as it is correct now * Added unit test for vector types Tests that hashes match for same values and that they don't match when changing a single bit on any of the vector elements * Changed hardcoded value for the compiler option * Release note --------- Co-authored-by: Christoffer Lerno <christoffer@aegik.com>
This commit is contained in:
@@ -174,6 +174,63 @@ fn void test_hash_repeat()
|
||||
assert(int.typeid.hash() == int.typeid.hash());
|
||||
}
|
||||
|
||||
macro test_hash_vector_macro(...)
|
||||
{
|
||||
int[] $lens = {1, 2, 4, 7, 8, 13, 16, 23, 32, 43, 64, 103};
|
||||
$for var $i = 0; $i < $vacount ; $i++:
|
||||
{
|
||||
$foreach $vec_len : $lens:
|
||||
{
|
||||
$if $vec_len * $vatype[$i].sizeof * 8 <= $$MAX_VECTOR_SIZE:
|
||||
{
|
||||
$vatype[$i][<$vec_len>] vec1, vec2;
|
||||
for (int val = 0; val < $vec_len; val++)
|
||||
{
|
||||
$vatype[$i] tval = ($vatype[$i])val;
|
||||
vec1[val] = (tval | (tval << (($vatype[$i].sizeof - 1) * 8)));
|
||||
}
|
||||
vec2 = vec1;
|
||||
assert(vec1.hash() == vec2.hash(), "hashes don't match for %s and %s", vec1, vec2);
|
||||
for (int val = 0; val < $vec_len; val++)
|
||||
{
|
||||
vec2[val] ^= (($vatype[$i])0b0010_0000 << (($vatype[$i].sizeof - 1) * 8));
|
||||
assert(vec1.hash() != vec2.hash(), "hashes match for %s and %s", vec1, vec2);
|
||||
vec2[val] ^= (($vatype[$i])0b0010_0000 << (($vatype[$i].sizeof - 1) * 8));
|
||||
}
|
||||
}
|
||||
$endif
|
||||
}
|
||||
$endforeach
|
||||
}
|
||||
$endfor
|
||||
}
|
||||
macro test_hash_vector_macro_bool()
|
||||
{
|
||||
int[] $lens = {1, 2, 4, 7, 8, 13, 16, 23, 32, 43, 64, 103};
|
||||
$foreach $vec_len : $lens:
|
||||
{
|
||||
bool[<$vec_len>] vec1, vec2;
|
||||
for (int val = 0; val < $vec_len; val++)
|
||||
{
|
||||
vec1[val] = ((val & 0x3) == 0);
|
||||
}
|
||||
vec2 = vec1;
|
||||
assert(vec1.hash() == vec2.hash(), "hashes don't match for %s and %s", vec1, vec2);
|
||||
for (int val = 0; val < $vec_len; val++)
|
||||
{
|
||||
vec2[val] = !vec2[val];
|
||||
assert(vec1.hash() != vec2.hash(), "hashes match for %s and %s", vec1, vec2);
|
||||
vec2[val] = !vec2[val];
|
||||
}
|
||||
}
|
||||
$endforeach
|
||||
}
|
||||
fn void test_hash_vector()
|
||||
{
|
||||
test_hash_vector_macro(char, ichar, short, ushort, int, uint, long, ulong, int128, uint128);
|
||||
test_hash_vector_macro_bool();
|
||||
}
|
||||
|
||||
fn void test_builtin_string_hashing() => @pool()
|
||||
{
|
||||
var $x = "";
|
||||
|
||||
Reference in New Issue
Block a user