diff options
author | Jonathan Gray <jsg@cvs.openbsd.org> | 2020-08-26 05:30:39 +0000 |
---|---|---|
committer | Jonathan Gray <jsg@cvs.openbsd.org> | 2020-08-26 05:30:39 +0000 |
commit | 27c93456b58343162f7c4ad20ca6bea0c9a91646 (patch) | |
tree | 945c20b63e0b9975ee40f114c5312f8d8f1a2d0b /lib/mesa/src/util/tests | |
parent | 875b83a3ee95e248388fbf72271acc80f6f97987 (diff) |
Import Mesa 20.1.6
Diffstat (limited to 'lib/mesa/src/util/tests')
19 files changed, 1208 insertions, 13 deletions
diff --git a/lib/mesa/src/util/tests/fast_idiv_by_const/fast_idiv_by_const_test.cpp b/lib/mesa/src/util/tests/fast_idiv_by_const/fast_idiv_by_const_test.cpp index b0b3669e7..330f90fa4 100644 --- a/lib/mesa/src/util/tests/fast_idiv_by_const/fast_idiv_by_const_test.cpp +++ b/lib/mesa/src/util/tests/fast_idiv_by_const/fast_idiv_by_const_test.cpp @@ -48,7 +48,7 @@ strunc(int64_t x, unsigned num_bits) if (num_bits == 64) return x; - return (x << (64 - num_bits)) >> (64 - num_bits); + return (int64_t)((uint64_t)x << (64 - num_bits)) >> (64 - num_bits); } static inline bool @@ -230,7 +230,7 @@ rand_sint(unsigned bits, unsigned min_abs) { /* Make sure we hit MIN_INT every once in a while */ if (rand() % 64 == 37) - return -(1 << (bits - 1)); + return INT64_MIN >> (64 - bits); int64_t s = rand_uint(bits - 1, min_abs); return rand() & 1 ? s : -s; @@ -306,7 +306,7 @@ random_sdiv_test(unsigned bits) int64_t d; do { d = rand_sint(bits, 2); - } while (util_is_power_of_two_or_zero64(llabs(d))); + } while (d == INT64_MIN || util_is_power_of_two_or_zero64(llabs(d))); assert(sint_is_in_range(n, bits)); assert(sint_is_in_range(d, bits) && llabs(d) >= 2); diff --git a/lib/mesa/src/util/tests/fast_idiv_by_const/meson.build b/lib/mesa/src/util/tests/fast_idiv_by_const/meson.build index 2a341d181..9c8c03d97 100644 --- a/lib/mesa/src/util/tests/fast_idiv_by_const/meson.build +++ b/lib/mesa/src/util/tests/fast_idiv_by_const/meson.build @@ -24,7 +24,8 @@ test( 'fast_idiv_by_const_test', 'fast_idiv_by_const_test.cpp', dependencies : [idep_gtest, idep_mesautil], - include_directories : inc_common, + include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux], ), suite : ['util'], + timeout: 60, ) diff --git a/lib/mesa/src/util/tests/fast_urem_by_const/meson.build b/lib/mesa/src/util/tests/fast_urem_by_const/meson.build index a16407d5e..079309745 100644 --- a/lib/mesa/src/util/tests/fast_urem_by_const/meson.build +++ b/lib/mesa/src/util/tests/fast_urem_by_const/meson.build @@ -25,7 +25,7 @@ test( 'fast_urem_by_const_test', 'fast_urem_by_const_test.cpp', dependencies : [idep_gtest, idep_mesautil], - include_directories : inc_common, + include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux], ), suite : ['util'], ) diff --git a/lib/mesa/src/util/tests/format/meson.build b/lib/mesa/src/util/tests/format/meson.build new file mode 100644 index 000000000..761ce49ab --- /dev/null +++ b/lib/mesa/src/util/tests/format/meson.build @@ -0,0 +1,12 @@ +foreach t : ['srgb', 'u_format_test', 'u_format_compatible_test'] + test(t, + executable( + t, + '@0@.c'.format(t), + include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux], + dependencies : idep_mesautil, + ), + suite : 'format', + should_fail : meson.get_cross_property('xfail', '').contains(t), + ) +endforeach diff --git a/lib/mesa/src/util/tests/format/srgb.c b/lib/mesa/src/util/tests/format/srgb.c new file mode 100644 index 000000000..9b3c15e65 --- /dev/null +++ b/lib/mesa/src/util/tests/format/srgb.c @@ -0,0 +1,40 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "util/macros.h" +#include "util/format/u_format.h" +#include "pipe/p_format.h" + +int main(void) +{ + for (enum pipe_format format = 0; format < PIPE_FORMAT_COUNT; format++) + { + if (!util_format_is_srgb(format)) { + const enum pipe_format linear = util_format_linear(format); + if (format != linear) { + fprintf(stderr, "%s converted to linear is %s\n", + util_format_name(format), + util_format_name(linear)); + return EXIT_FAILURE; + } + continue; + } + + const enum pipe_format linear = util_format_linear(format); + if (format == linear) { + fprintf(stderr, "%s can't be converted to a linear equivalent\n", + util_format_name(format)); + return EXIT_FAILURE; + } + + const enum pipe_format srgb = util_format_srgb(linear); + if (format != srgb) { + fprintf(stderr, "%s converted to linear and back to srgb becomes %s\n", + util_format_name(format), + util_format_name(srgb)); + return EXIT_FAILURE; + } + } + + return EXIT_SUCCESS; +} diff --git a/lib/mesa/src/util/tests/format/u_format_compatible_test.c b/lib/mesa/src/util/tests/format/u_format_compatible_test.c new file mode 100644 index 000000000..6d3ce5efa --- /dev/null +++ b/lib/mesa/src/util/tests/format/u_format_compatible_test.c @@ -0,0 +1,76 @@ +/************************************************************************** + * + * Copyright 2009-2010 VMware, Inc. + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. + * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + **************************************************************************/ + + +#include <stdlib.h> +#include <stdio.h> + +#include "util/format/u_format.h" + + +static boolean +test_all(void) +{ + enum pipe_format src_format; + enum pipe_format dst_format; + + for (src_format = 1; src_format < PIPE_FORMAT_COUNT; ++src_format) { + const struct util_format_description *src_format_desc; + src_format_desc = util_format_description(src_format); + if (!src_format_desc) { + continue; + } + + for (dst_format = 1; dst_format < PIPE_FORMAT_COUNT; ++dst_format) { + const struct util_format_description *dst_format_desc; + dst_format_desc = util_format_description(dst_format); + if (!dst_format_desc) { + continue; + } + + if (dst_format == src_format) { + continue; + } + + if (util_is_format_compatible(src_format_desc, dst_format_desc)) { + printf("%s -> %s\n", src_format_desc->short_name, dst_format_desc->short_name); + } + } + } + + return TRUE; +} + + +int main(int argc, char **argv) +{ + boolean success; + + success = test_all(); + + return success ? 0 : 1; +} diff --git a/lib/mesa/src/util/tests/format/u_format_test.c b/lib/mesa/src/util/tests/format/u_format_test.c new file mode 100644 index 000000000..69e3b5aeb --- /dev/null +++ b/lib/mesa/src/util/tests/format/u_format_test.c @@ -0,0 +1,819 @@ +/************************************************************************** + * + * Copyright 2009-2010 VMware, Inc. + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. + * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + **************************************************************************/ + + +#include <stdlib.h> +#include <stdio.h> +#include <float.h> + +#include "util/u_half.h" +#include "util/format/u_format.h" +#include "util/format/u_format_tests.h" +#include "util/format/u_format_s3tc.h" + + +static boolean +compare_float(float x, float y) +{ + float error = y - x; + + if (error < 0.0f) + error = -error; + + if (error > FLT_EPSILON) { + return FALSE; + } + + return TRUE; +} + + +static void +print_packed(const struct util_format_description *format_desc, + const char *prefix, + const uint8_t *packed, + const char *suffix) +{ + unsigned i; + const char *sep = ""; + + printf("%s", prefix); + for (i = 0; i < format_desc->block.bits/8; ++i) { + printf("%s%02x", sep, packed[i]); + sep = " "; + } + printf("%s", suffix); + fflush(stdout); +} + + +static void +print_unpacked_rgba_doubl(const struct util_format_description *format_desc, + const char *prefix, + const double unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4], + const char *suffix) +{ + unsigned i, j; + const char *sep = ""; + + printf("%s", prefix); + for (i = 0; i < format_desc->block.height; ++i) { + for (j = 0; j < format_desc->block.width; ++j) { + printf("%s{%f, %f, %f, %f}", sep, unpacked[i][j][0], unpacked[i][j][1], unpacked[i][j][2], unpacked[i][j][3]); + sep = ", "; + } + sep = ",\n"; + } + printf("%s", suffix); + fflush(stdout); +} + + +static void +print_unpacked_rgba_float(const struct util_format_description *format_desc, + const char *prefix, + float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4], + const char *suffix) +{ + unsigned i, j; + const char *sep = ""; + + printf("%s", prefix); + for (i = 0; i < format_desc->block.height; ++i) { + for (j = 0; j < format_desc->block.width; ++j) { + printf("%s{%f, %f, %f, %f}", sep, unpacked[i][j][0], unpacked[i][j][1], unpacked[i][j][2], unpacked[i][j][3]); + sep = ", "; + } + sep = ",\n"; + } + printf("%s", suffix); + fflush(stdout); +} + + +static void +print_unpacked_rgba_8unorm(const struct util_format_description *format_desc, + const char *prefix, + uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4], + const char *suffix) +{ + unsigned i, j; + const char *sep = ""; + + printf("%s", prefix); + for (i = 0; i < format_desc->block.height; ++i) { + for (j = 0; j < format_desc->block.width; ++j) { + printf("%s{0x%02x, 0x%02x, 0x%02x, 0x%02x}", sep, unpacked[i][j][0], unpacked[i][j][1], unpacked[i][j][2], unpacked[i][j][3]); + sep = ", "; + } + } + printf("%s", suffix); + fflush(stdout); +} + + +static void +print_unpacked_z_float(const struct util_format_description *format_desc, + const char *prefix, + float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH], + const char *suffix) +{ + unsigned i, j; + const char *sep = ""; + + printf("%s", prefix); + for (i = 0; i < format_desc->block.height; ++i) { + for (j = 0; j < format_desc->block.width; ++j) { + printf("%s%f", sep, unpacked[i][j]); + sep = ", "; + } + sep = ",\n"; + } + printf("%s", suffix); + fflush(stdout); +} + + +static void +print_unpacked_z_32unorm(const struct util_format_description *format_desc, + const char *prefix, + uint32_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH], + const char *suffix) +{ + unsigned i, j; + const char *sep = ""; + + printf("%s", prefix); + for (i = 0; i < format_desc->block.height; ++i) { + for (j = 0; j < format_desc->block.width; ++j) { + printf("%s0x%08x", sep, unpacked[i][j]); + sep = ", "; + } + } + printf("%s", suffix); + fflush(stdout); +} + + +static void +print_unpacked_s_8uint(const struct util_format_description *format_desc, + const char *prefix, + uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH], + const char *suffix) +{ + unsigned i, j; + const char *sep = ""; + + printf("%s", prefix); + for (i = 0; i < format_desc->block.height; ++i) { + for (j = 0; j < format_desc->block.width; ++j) { + printf("%s0x%02x", sep, unpacked[i][j]); + sep = ", "; + } + } + printf("%s", suffix); + fflush(stdout); +} + + +static boolean +test_format_fetch_rgba_float(const struct util_format_description *format_desc, + const struct util_format_test_case *test) +{ + float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4] = { { { 0 } } }; + unsigned i, j, k; + boolean success; + + success = TRUE; + for (i = 0; i < format_desc->block.height; ++i) { + for (j = 0; j < format_desc->block.width; ++j) { + format_desc->fetch_rgba_float(unpacked[i][j], test->packed, j, i); + for (k = 0; k < 4; ++k) { + if (!compare_float(test->unpacked[i][j][k], unpacked[i][j][k])) { + success = FALSE; + } + } + } + } + + /* Ignore S3TC errors */ + if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) { + success = TRUE; + } + + if (!success) { + print_unpacked_rgba_float(format_desc, "FAILED: ", unpacked, " obtained\n"); + print_unpacked_rgba_doubl(format_desc, " ", test->unpacked, " expected\n"); + } + + return success; +} + + +static boolean +test_format_unpack_rgba_float(const struct util_format_description *format_desc, + const struct util_format_test_case *test) +{ + float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4] = { { { 0 } } }; + unsigned i, j, k; + boolean success; + + format_desc->unpack_rgba_float(&unpacked[0][0][0], sizeof unpacked[0], + test->packed, 0, + format_desc->block.width, format_desc->block.height); + + success = TRUE; + for (i = 0; i < format_desc->block.height; ++i) { + for (j = 0; j < format_desc->block.width; ++j) { + for (k = 0; k < 4; ++k) { + if (!compare_float(test->unpacked[i][j][k], unpacked[i][j][k])) { + success = FALSE; + } + } + } + } + + /* Ignore S3TC errors */ + if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) { + success = TRUE; + } + + if (!success) { + print_unpacked_rgba_float(format_desc, "FAILED: ", unpacked, " obtained\n"); + print_unpacked_rgba_doubl(format_desc, " ", test->unpacked, " expected\n"); + } + + return success; +} + + +static boolean +test_format_pack_rgba_float(const struct util_format_description *format_desc, + const struct util_format_test_case *test) +{ + float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4]; + uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES]; + unsigned i, j, k; + boolean success; + + if (test->format == PIPE_FORMAT_DXT1_RGBA) { + /* + * Skip S3TC as packed representation is not canonical. + * + * TODO: Do a round trip conversion. + */ + return TRUE; + } + + memset(packed, 0, sizeof packed); + for (i = 0; i < format_desc->block.height; ++i) { + for (j = 0; j < format_desc->block.width; ++j) { + for (k = 0; k < 4; ++k) { + unpacked[i][j][k] = (float) test->unpacked[i][j][k]; + } + } + } + + format_desc->pack_rgba_float(packed, 0, + &unpacked[0][0][0], sizeof unpacked[0], + format_desc->block.width, format_desc->block.height); + + success = TRUE; + for (i = 0; i < format_desc->block.bits/8; ++i) { + if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i])) + success = FALSE; + } + + /* Ignore NaN */ + if (util_is_double_nan(test->unpacked[0][0][0])) + success = TRUE; + + /* Ignore S3TC errors */ + if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) { + success = TRUE; + } + + if (!success) { + print_packed(format_desc, "FAILED: ", packed, " obtained\n"); + print_packed(format_desc, " ", test->packed, " expected\n"); + } + + return success; +} + + +static boolean +convert_float_to_8unorm(uint8_t *dst, const double *src) +{ + unsigned i; + boolean accurate = TRUE; + + for (i = 0; i < UTIL_FORMAT_MAX_UNPACKED_HEIGHT*UTIL_FORMAT_MAX_UNPACKED_WIDTH*4; ++i) { + if (src[i] < 0.0) { + accurate = FALSE; + dst[i] = 0; + } + else if (src[i] > 1.0) { + accurate = FALSE; + dst[i] = 255; + } + else { + dst[i] = src[i] * 255.0; + } + } + + return accurate; +} + + +static boolean +test_format_unpack_rgba_8unorm(const struct util_format_description *format_desc, + const struct util_format_test_case *test) +{ + uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4] = { { { 0 } } }; + uint8_t expected[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4] = { { { 0 } } }; + unsigned i, j, k; + boolean success; + + format_desc->unpack_rgba_8unorm(&unpacked[0][0][0], sizeof unpacked[0], + test->packed, 0, + format_desc->block.width, format_desc->block.height); + + convert_float_to_8unorm(&expected[0][0][0], &test->unpacked[0][0][0]); + + success = TRUE; + for (i = 0; i < format_desc->block.height; ++i) { + for (j = 0; j < format_desc->block.width; ++j) { + for (k = 0; k < 4; ++k) { + if (expected[i][j][k] != unpacked[i][j][k]) { + success = FALSE; + } + } + } + } + + /* Ignore NaN */ + if (util_is_double_nan(test->unpacked[0][0][0])) + success = TRUE; + + if (!success) { + print_unpacked_rgba_8unorm(format_desc, "FAILED: ", unpacked, " obtained\n"); + print_unpacked_rgba_8unorm(format_desc, " ", expected, " expected\n"); + } + + return success; +} + + +static boolean +test_format_pack_rgba_8unorm(const struct util_format_description *format_desc, + const struct util_format_test_case *test) +{ + uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4]; + uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES]; + unsigned i; + boolean success; + + if (test->format == PIPE_FORMAT_DXT1_RGBA) { + /* + * Skip S3TC as packed representation is not canonical. + * + * TODO: Do a round trip conversion. + */ + return TRUE; + } + + if (!convert_float_to_8unorm(&unpacked[0][0][0], &test->unpacked[0][0][0])) { + /* + * Skip test cases which cannot be represented by four unorm bytes. + */ + return TRUE; + } + + memset(packed, 0, sizeof packed); + + format_desc->pack_rgba_8unorm(packed, 0, + &unpacked[0][0][0], sizeof unpacked[0], + format_desc->block.width, format_desc->block.height); + + success = TRUE; + for (i = 0; i < format_desc->block.bits/8; ++i) + if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i])) + success = FALSE; + + /* Ignore NaN */ + if (util_is_double_nan(test->unpacked[0][0][0])) + success = TRUE; + + /* Ignore failure cases due to unorm8 format */ + if (test->unpacked[0][0][0] > 1.0f || test->unpacked[0][0][0] < 0.0f) + success = TRUE; + + /* Multiple of 255 */ + if ((test->unpacked[0][0][0] * 255.0) != (int)(test->unpacked[0][0][0] * 255.0)) + success = TRUE; + + /* Ignore S3TC errors */ + if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) { + success = TRUE; + } + + if (!success) { + print_packed(format_desc, "FAILED: ", packed, " obtained\n"); + print_packed(format_desc, " ", test->packed, " expected\n"); + } + + return success; +} + + +static boolean +test_format_unpack_z_float(const struct util_format_description *format_desc, + const struct util_format_test_case *test) +{ + float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH] = { { 0 } }; + unsigned i, j; + boolean success; + + format_desc->unpack_z_float(&unpacked[0][0], sizeof unpacked[0], + test->packed, 0, + format_desc->block.width, format_desc->block.height); + + success = TRUE; + for (i = 0; i < format_desc->block.height; ++i) { + for (j = 0; j < format_desc->block.width; ++j) { + if (!compare_float(test->unpacked[i][j][0], unpacked[i][j])) { + success = FALSE; + } + } + } + + if (!success) { + print_unpacked_z_float(format_desc, "FAILED: ", unpacked, " obtained\n"); + print_unpacked_rgba_doubl(format_desc, " ", test->unpacked, " expected\n"); + } + + return success; +} + + +static boolean +test_format_pack_z_float(const struct util_format_description *format_desc, + const struct util_format_test_case *test) +{ + float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH]; + uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES]; + unsigned i, j; + boolean success; + + memset(packed, 0, sizeof packed); + for (i = 0; i < format_desc->block.height; ++i) { + for (j = 0; j < format_desc->block.width; ++j) { + unpacked[i][j] = (float) test->unpacked[i][j][0]; + if (test->unpacked[i][j][1]) { + return TRUE; + } + } + } + + format_desc->pack_z_float(packed, 0, + &unpacked[0][0], sizeof unpacked[0], + format_desc->block.width, format_desc->block.height); + + success = TRUE; + for (i = 0; i < format_desc->block.bits/8; ++i) + if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i])) + success = FALSE; + + if (!success) { + print_packed(format_desc, "FAILED: ", packed, " obtained\n"); + print_packed(format_desc, " ", test->packed, " expected\n"); + } + + return success; +} + + +static boolean +test_format_unpack_z_32unorm(const struct util_format_description *format_desc, + const struct util_format_test_case *test) +{ + uint32_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH] = { { 0 } }; + uint32_t expected[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH] = { { 0 } }; + unsigned i, j; + boolean success; + + format_desc->unpack_z_32unorm(&unpacked[0][0], sizeof unpacked[0], + test->packed, 0, + format_desc->block.width, format_desc->block.height); + + for (i = 0; i < format_desc->block.height; ++i) { + for (j = 0; j < format_desc->block.width; ++j) { + expected[i][j] = test->unpacked[i][j][0] * 0xffffffff; + } + } + + success = TRUE; + for (i = 0; i < format_desc->block.height; ++i) { + for (j = 0; j < format_desc->block.width; ++j) { + if (expected[i][j] != unpacked[i][j]) { + success = FALSE; + } + } + } + + if (!success) { + print_unpacked_z_32unorm(format_desc, "FAILED: ", unpacked, " obtained\n"); + print_unpacked_z_32unorm(format_desc, " ", expected, " expected\n"); + } + + return success; +} + + +static boolean +test_format_pack_z_32unorm(const struct util_format_description *format_desc, + const struct util_format_test_case *test) +{ + uint32_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH]; + uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES]; + unsigned i, j; + boolean success; + + for (i = 0; i < format_desc->block.height; ++i) { + for (j = 0; j < format_desc->block.width; ++j) { + unpacked[i][j] = test->unpacked[i][j][0] * 0xffffffff; + if (test->unpacked[i][j][1]) { + return TRUE; + } + } + } + + memset(packed, 0, sizeof packed); + + format_desc->pack_z_32unorm(packed, 0, + &unpacked[0][0], sizeof unpacked[0], + format_desc->block.width, format_desc->block.height); + + success = TRUE; + for (i = 0; i < format_desc->block.bits/8; ++i) + if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i])) + success = FALSE; + + if (!success) { + print_packed(format_desc, "FAILED: ", packed, " obtained\n"); + print_packed(format_desc, " ", test->packed, " expected\n"); + } + + return success; +} + + +static boolean +test_format_unpack_s_8uint(const struct util_format_description *format_desc, + const struct util_format_test_case *test) +{ + uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH] = { { 0 } }; + uint8_t expected[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH] = { { 0 } }; + unsigned i, j; + boolean success; + + format_desc->unpack_s_8uint(&unpacked[0][0], sizeof unpacked[0], + test->packed, 0, + format_desc->block.width, format_desc->block.height); + + for (i = 0; i < format_desc->block.height; ++i) { + for (j = 0; j < format_desc->block.width; ++j) { + expected[i][j] = test->unpacked[i][j][1]; + } + } + + success = TRUE; + for (i = 0; i < format_desc->block.height; ++i) { + for (j = 0; j < format_desc->block.width; ++j) { + if (expected[i][j] != unpacked[i][j]) { + success = FALSE; + } + } + } + + if (!success) { + print_unpacked_s_8uint(format_desc, "FAILED: ", unpacked, " obtained\n"); + print_unpacked_s_8uint(format_desc, " ", expected, " expected\n"); + } + + return success; +} + + +static boolean +test_format_pack_s_8uint(const struct util_format_description *format_desc, + const struct util_format_test_case *test) +{ + uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH]; + uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES]; + unsigned i, j; + boolean success; + + for (i = 0; i < format_desc->block.height; ++i) { + for (j = 0; j < format_desc->block.width; ++j) { + unpacked[i][j] = test->unpacked[i][j][1]; + if (test->unpacked[i][j][0]) { + return TRUE; + } + } + } + + memset(packed, 0, sizeof packed); + + format_desc->pack_s_8uint(packed, 0, + &unpacked[0][0], sizeof unpacked[0], + format_desc->block.width, format_desc->block.height); + + success = TRUE; + for (i = 0; i < format_desc->block.bits/8; ++i) + if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i])) + success = FALSE; + + if (!success) { + print_packed(format_desc, "FAILED: ", packed, " obtained\n"); + print_packed(format_desc, " ", test->packed, " expected\n"); + } + + return success; +} + + +/* Touch-test that the unorm/snorm flags are set up right by codegen. */ +static boolean +test_format_norm_flags(const struct util_format_description *format_desc) +{ + boolean success = TRUE; + +#define FORMAT_CASE(format, unorm, snorm) \ + case format: \ + success = (format_desc->is_unorm == unorm && \ + format_desc->is_snorm == snorm); \ + break + + switch (format_desc->format) { + FORMAT_CASE(PIPE_FORMAT_R8G8B8A8_UNORM, TRUE, FALSE); + FORMAT_CASE(PIPE_FORMAT_R8G8B8A8_SRGB, TRUE, FALSE); + FORMAT_CASE(PIPE_FORMAT_R8G8B8A8_SNORM, FALSE, TRUE); + FORMAT_CASE(PIPE_FORMAT_R32_FLOAT, FALSE, FALSE); + FORMAT_CASE(PIPE_FORMAT_X8Z24_UNORM, TRUE, FALSE); + FORMAT_CASE(PIPE_FORMAT_S8X24_UINT, FALSE, FALSE); + FORMAT_CASE(PIPE_FORMAT_DXT1_RGB, TRUE, FALSE); + FORMAT_CASE(PIPE_FORMAT_ETC2_RGB8, TRUE, FALSE); + FORMAT_CASE(PIPE_FORMAT_ETC2_R11_SNORM, FALSE, TRUE); + FORMAT_CASE(PIPE_FORMAT_ASTC_4x4, TRUE, FALSE); + FORMAT_CASE(PIPE_FORMAT_BPTC_RGBA_UNORM, TRUE, FALSE); + FORMAT_CASE(PIPE_FORMAT_BPTC_RGB_FLOAT, FALSE, FALSE); + default: + success = !(format_desc->is_unorm && format_desc->is_snorm); + break; + } +#undef FORMAT_CASE + + if (!success) { + printf("FAILED: %s (unorm %s, snorm %s)\n", + format_desc->short_name, + format_desc->is_unorm ? "yes" : "no", + format_desc->is_snorm ? "yes" : "no"); + } + + return success; +} + +typedef boolean +(*test_func_t)(const struct util_format_description *format_desc, + const struct util_format_test_case *test); + + +static boolean +test_one_func(const struct util_format_description *format_desc, + test_func_t func, + const char *suffix) +{ + unsigned i; + boolean success = TRUE; + + printf("Testing util_format_%s_%s ...\n", + format_desc->short_name, suffix); + fflush(stdout); + + for (i = 0; i < util_format_nr_test_cases; ++i) { + const struct util_format_test_case *test = &util_format_test_cases[i]; + + if (test->format == format_desc->format) { + if (!func(format_desc, &util_format_test_cases[i])) { + success = FALSE; + } + } + } + + return success; +} + +static boolean +test_format_metadata(const struct util_format_description *format_desc, + boolean (*func)(const struct util_format_description *format_desc), + const char *suffix) +{ + boolean success = TRUE; + + printf("Testing util_format_%s_%s ...\n", format_desc->short_name, suffix); + fflush(stdout); + + if (!func(format_desc)) { + success = FALSE; + } + + return success; +} + +static boolean +test_all(void) +{ + enum pipe_format format; + boolean success = TRUE; + + for (format = 1; format < PIPE_FORMAT_COUNT; ++format) { + const struct util_format_description *format_desc; + + format_desc = util_format_description(format); + if (!format_desc) { + continue; + } + + assert(format_desc->block.bits <= UTIL_FORMAT_MAX_PACKED_BYTES * 8); + assert(format_desc->block.height <= UTIL_FORMAT_MAX_UNPACKED_HEIGHT); + assert(format_desc->block.width <= UTIL_FORMAT_MAX_UNPACKED_WIDTH); + +# define TEST_ONE_FUNC(name) \ + if (format_desc->name) { \ + if (!test_one_func(format_desc, &test_format_##name, #name)) { \ + success = FALSE; \ + } \ + } + +# define TEST_FORMAT_METADATA(name) \ + if (!test_format_metadata(format_desc, &test_format_##name, #name)) { \ + success = FALSE; \ + } \ + + TEST_ONE_FUNC(fetch_rgba_float); + TEST_ONE_FUNC(pack_rgba_float); + TEST_ONE_FUNC(unpack_rgba_float); + TEST_ONE_FUNC(pack_rgba_8unorm); + TEST_ONE_FUNC(unpack_rgba_8unorm); + + TEST_ONE_FUNC(unpack_z_32unorm); + TEST_ONE_FUNC(pack_z_32unorm); + TEST_ONE_FUNC(unpack_z_float); + TEST_ONE_FUNC(pack_z_float); + TEST_ONE_FUNC(unpack_s_8uint); + TEST_ONE_FUNC(pack_s_8uint); + + TEST_FORMAT_METADATA(norm_flags); + +# undef TEST_ONE_FUNC +# undef TEST_ONE_FORMAT + } + + return success; +} + + +int main(int argc, char **argv) +{ + boolean success; + + success = test_all(); + + return success ? 0 : 1; +} diff --git a/lib/mesa/src/util/tests/hash_table/destroy_callback.c b/lib/mesa/src/util/tests/hash_table/destroy_callback.c index 3a050ff2a..e6c7ac5c1 100644 --- a/lib/mesa/src/util/tests/hash_table/destroy_callback.c +++ b/lib/mesa/src/util/tests/hash_table/destroy_callback.c @@ -56,7 +56,7 @@ main(int argc, char **argv) (void) argc; (void) argv; - ht = _mesa_hash_table_create(NULL, _mesa_key_hash_string, + ht = _mesa_hash_table_create(NULL, _mesa_hash_string, _mesa_key_string_equal); _mesa_hash_table_insert(ht, str1, NULL); diff --git a/lib/mesa/src/util/tests/hash_table/insert_and_lookup.c b/lib/mesa/src/util/tests/hash_table/insert_and_lookup.c index 1fd735368..f12446490 100644 --- a/lib/mesa/src/util/tests/hash_table/insert_and_lookup.c +++ b/lib/mesa/src/util/tests/hash_table/insert_and_lookup.c @@ -43,7 +43,7 @@ main(int argc, char **argv) (void) argc; (void) argv; - ht = _mesa_hash_table_create(NULL, _mesa_key_hash_string, + ht = _mesa_hash_table_create(NULL, _mesa_hash_string, _mesa_key_string_equal); _mesa_hash_table_insert(ht, str1, NULL); diff --git a/lib/mesa/src/util/tests/hash_table/random_entry.c b/lib/mesa/src/util/tests/hash_table/random_entry.c index 4902a999d..75c4ef652 100644 --- a/lib/mesa/src/util/tests/hash_table/random_entry.c +++ b/lib/mesa/src/util/tests/hash_table/random_entry.c @@ -58,7 +58,7 @@ main(int argc, char **argv) struct hash_table *ht; struct hash_entry *entry; uint32_t keys[SIZE]; - uint32_t i, random_value; + uint32_t i, random_value = 0; (void) argc; (void) argv; diff --git a/lib/mesa/src/util/tests/hash_table/remove_key.c b/lib/mesa/src/util/tests/hash_table/remove_key.c index 6df6d7d03..1abc68dfc 100644 --- a/lib/mesa/src/util/tests/hash_table/remove_key.c +++ b/lib/mesa/src/util/tests/hash_table/remove_key.c @@ -40,7 +40,7 @@ main(int argc, char **argv) (void) argc; (void) argv; - ht = _mesa_hash_table_create(NULL, _mesa_key_hash_string, _mesa_key_string_equal); + ht = _mesa_hash_table_create(NULL, _mesa_hash_string, _mesa_key_string_equal); _mesa_hash_table_insert(ht, str1, NULL); _mesa_hash_table_insert(ht, str2, NULL); diff --git a/lib/mesa/src/util/tests/hash_table/replacement.c b/lib/mesa/src/util/tests/hash_table/replacement.c index e74e63453..a3759a3c9 100644 --- a/lib/mesa/src/util/tests/hash_table/replacement.c +++ b/lib/mesa/src/util/tests/hash_table/replacement.c @@ -45,7 +45,7 @@ main(int argc, char **argv) assert(str1 != str2); - ht = _mesa_hash_table_create(NULL, _mesa_key_hash_string, + ht = _mesa_hash_table_create(NULL, _mesa_hash_string, _mesa_key_string_equal); _mesa_hash_table_insert(ht, str1, str1); diff --git a/lib/mesa/src/util/tests/set/meson.build b/lib/mesa/src/util/tests/set/meson.build index 9d0d311ba..e9b00629b 100644 --- a/lib/mesa/src/util/tests/set/meson.build +++ b/lib/mesa/src/util/tests/set/meson.build @@ -24,7 +24,7 @@ test( 'set_test', 'set_test.cpp', dependencies : [dep_thread, dep_dl, idep_gtest, idep_mesautil], - include_directories : inc_common, + include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux], ), suite : ['util'], ) diff --git a/lib/mesa/src/util/tests/sparse_array/meson.build b/lib/mesa/src/util/tests/sparse_array/meson.build new file mode 100644 index 000000000..9afb49301 --- /dev/null +++ b/lib/mesa/src/util/tests/sparse_array/meson.build @@ -0,0 +1,31 @@ +# Copyright © 2019 Intel Corporation + +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: + +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. + +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +test( + 'sparse_array_multi_threaded', + executable( + 'multi_threaded', + 'multi_threaded.c', + dependencies : [idep_mesautil], + include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux], + ), + suite : ['util'], + timeout: 60, +) diff --git a/lib/mesa/src/util/tests/sparse_array/multi_threaded.c b/lib/mesa/src/util/tests/sparse_array/multi_threaded.c new file mode 100644 index 000000000..b084ac994 --- /dev/null +++ b/lib/mesa/src/util/tests/sparse_array/multi_threaded.c @@ -0,0 +1,84 @@ +/* + * Copyright © 2019 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#undef NDEBUG + +#include "util/sparse_array.h" + +#include <assert.h> +#include <stdlib.h> +#include "c11/threads.h" + +#define NUM_THREADS 16 +#define NUM_RUNS 16 +#define NUM_SETS_PER_THREAD (1 << 10) +#define MAX_ARR_SIZE (1 << 20) + +static int +test_thread(void *_state) +{ + struct util_sparse_array *arr = _state; + for (unsigned i = 0; i < NUM_SETS_PER_THREAD; i++) { + uint32_t idx = rand() % MAX_ARR_SIZE; + uint32_t *elem = util_sparse_array_get(arr, idx); + *elem = idx; + } + + return 0; +} + +static void +run_test(unsigned run_idx) +{ + size_t node_size = 4 << (run_idx / 2); + + struct util_sparse_array arr; + util_sparse_array_init(&arr, sizeof(uint32_t), node_size); + + thrd_t threads[NUM_THREADS]; + for (unsigned i = 0; i < NUM_THREADS; i++) { + int ret = thrd_create(&threads[i], test_thread, &arr); + assert(ret == thrd_success); + } + + for (unsigned i = 0; i < NUM_THREADS; i++) { + int ret = thrd_join(threads[i], NULL); + assert(ret == thrd_success); + } + + util_sparse_array_validate(&arr); + + for (unsigned i = 0; i < MAX_ARR_SIZE; i++) { + uint32_t *elem = util_sparse_array_get(&arr, i); + assert(*elem == 0 || *elem == i); + } + + util_sparse_array_finish(&arr); +} + +int +main(int argc, char **argv) +{ + for (unsigned i = 0; i < NUM_RUNS; i++) + run_test(i); +} diff --git a/lib/mesa/src/util/tests/string_buffer/meson.build b/lib/mesa/src/util/tests/string_buffer/meson.build index cb767bb31..acb6abca4 100644 --- a/lib/mesa/src/util/tests/string_buffer/meson.build +++ b/lib/mesa/src/util/tests/string_buffer/meson.build @@ -23,8 +23,9 @@ test( executable( 'string_buffer_test', 'string_buffer_test.cpp', + cpp_args : [cpp_msvc_compat_args], dependencies : [idep_gtest, idep_mesautil], - include_directories : inc_common, + include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux], ), suite : ['util'], ) diff --git a/lib/mesa/src/util/tests/timespec/meson.build b/lib/mesa/src/util/tests/timespec/meson.build index c685db5fd..2fc737a7d 100644 --- a/lib/mesa/src/util/tests/timespec/meson.build +++ b/lib/mesa/src/util/tests/timespec/meson.build @@ -24,7 +24,7 @@ test( 'timespec_test', 'timespec_test.cpp', dependencies : [dep_thread, dep_dl, idep_gtest, idep_mesautil], - include_directories : inc_common, + include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux], ), suite : ['util'], ) diff --git a/lib/mesa/src/util/tests/vector/meson.build b/lib/mesa/src/util/tests/vector/meson.build new file mode 100644 index 000000000..40ad93995 --- /dev/null +++ b/lib/mesa/src/util/tests/vector/meson.build @@ -0,0 +1,30 @@ +# Copyright © 2020 Google, LLC + +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: + +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. + +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +test( + 'vector', + executable( + 'vector_test', + 'vector_test.cpp', + dependencies : [idep_gtest, idep_mesautil], + include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux], + ), + suite : ['util'], +) diff --git a/lib/mesa/src/util/tests/vector/vector_test.cpp b/lib/mesa/src/util/tests/vector/vector_test.cpp new file mode 100644 index 000000000..aa7ca2bbf --- /dev/null +++ b/lib/mesa/src/util/tests/vector/vector_test.cpp @@ -0,0 +1,101 @@ +/* + * Copyright © 2019 Google, LLC + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "util/u_vector.h" +#include "gtest/gtest.h" + +static void test(uint32_t size_in_elements, uint32_t elements_to_walk, uint32_t start) +{ + struct u_vector vector; + uint32_t add_counter = 0; + uint32_t remove_counter = 0; + + ASSERT_TRUE(u_vector_init(&vector, sizeof(uint64_t), sizeof(uint64_t) * size_in_elements)); + + // Override the head and tail so we can quickly test rollover + vector.head = vector.tail = start; + + EXPECT_EQ(sizeof(uint64_t) * size_in_elements, vector.size); + EXPECT_EQ(0, u_vector_length(&vector)); + + for (uint32_t i = 0; i < size_in_elements; i++) { + *(uint64_t*)u_vector_add(&vector) = add_counter++; + + int length = u_vector_length(&vector); + EXPECT_EQ(i + 1, length); + + // Check the entries + uint32_t count = 0; + void* element; + u_vector_foreach(element, &vector) + { + EXPECT_EQ(count, *(uint64_t*)element) << "i: " << i << " count: " << count; + count++; + } + EXPECT_EQ(count, length); + } + + // Remove + add + for (uint32_t i = 0; i < elements_to_walk; i++) { + u_vector_remove(&vector); + remove_counter++; + *(uint64_t*)u_vector_add(&vector) = add_counter++; + } + + EXPECT_EQ(sizeof(uint64_t) * size_in_elements, vector.size); + + // Grow the vector now + *(uint64_t*)u_vector_add(&vector) = add_counter++; + EXPECT_EQ(size_in_elements + 1, u_vector_length(&vector)); + + EXPECT_EQ(sizeof(uint64_t) * size_in_elements * 2, vector.size); + + { + uint32_t count = remove_counter; + void* element; + u_vector_foreach(element, &vector) + { + EXPECT_EQ(count++, *(uint64_t*)element) << "count: " << count; + } + } + + u_vector_finish(&vector); +} + +TEST(Vector, Grow0) { test(4, 0, 0); } + +TEST(Vector, Grow1) { test(4, 1, 0); } + +TEST(Vector, Grow2) { test(4, 2, 0); } + +TEST(Vector, Grow3) { test(4, 3, 0); } + +TEST(Vector, Grow4) { test(4, 4, 0); } + +TEST(Vector, Grow5) { test(4, 5, 0); } + +TEST(Vector, Rollover) +{ + uint32_t start = (1ull << 32) - 4 * sizeof(uint64_t); + test(8, 4, start); +} |