Mesa (staging/20.1): anv/tests: Don't rely on assert or changing NDEBUG in tests

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Tue May 19 14:11:51 UTC 2020


Module: Mesa
Branch: staging/20.1
Commit: 2e7515faea5d173d36b859b4e72c8eb3314a1325
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=2e7515faea5d173d36b859b4e72c8eb3314a1325

Author: Ian Romanick <ian.d.romanick at intel.com>
Date:   Mon May 11 12:33:24 2020 -0700

anv/tests: Don't rely on assert or changing NDEBUG in tests

This is the last part of the fix for #2903.

v2: Add test_common.h.

Fixes: f7c56475d25 ("anv/tests: compile to something sensible in release builds")
Reviewed-by: Jason Ekstrand <jason at jlekstrand.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4994>
(cherry picked from commit f4638cfdad3199bd97cf8ca7070008186bff456a)

---

 .pick_status.json                                  |  2 +-
 src/intel/vulkan/tests/block_pool_grow_first.c     | 13 ++++-----
 src/intel/vulkan/tests/block_pool_no_free.c        | 13 ++++-----
 src/intel/vulkan/tests/state_pool.c                |  3 +-
 src/intel/vulkan/tests/state_pool_free_list_only.c |  5 ++--
 src/intel/vulkan/tests/state_pool_no_free.c        |  5 ++--
 src/intel/vulkan/tests/state_pool_padding.c        | 15 +++++-----
 src/intel/vulkan/tests/state_pool_test_helper.h    |  2 +-
 src/intel/vulkan/tests/test_common.h               | 34 ++++++++++++++++++++++
 9 files changed, 60 insertions(+), 32 deletions(-)

diff --git a/.pick_status.json b/.pick_status.json
index 2862d0b3dd3..9b8f9d8fa9b 100644
--- a/.pick_status.json
+++ b/.pick_status.json
@@ -1300,7 +1300,7 @@
         "description": "anv/tests: Don't rely on assert or changing NDEBUG in tests",
         "nominated": true,
         "nomination_type": 1,
-        "resolution": 0,
+        "resolution": 1,
         "master_sha": null,
         "because_sha": "f7c56475d25138234ab0bb28a10df9000de594f9"
     },
diff --git a/src/intel/vulkan/tests/block_pool_grow_first.c b/src/intel/vulkan/tests/block_pool_grow_first.c
index 0c300a91059..073587dc7f7 100644
--- a/src/intel/vulkan/tests/block_pool_grow_first.c
+++ b/src/intel/vulkan/tests/block_pool_grow_first.c
@@ -21,9 +21,8 @@
  * IN THE SOFTWARE.
  */
 
-#undef NDEBUG
-
 #include "anv_private.h"
+#include "test_common.h"
 
 int main(int argc, char **argv)
 {
@@ -44,20 +43,20 @@ int main(int argc, char **argv)
    pthread_mutex_init(&device.mutex, NULL);
    anv_bo_cache_init(&device.bo_cache);
    anv_block_pool_init(&pool, &device, 4096, initial_size);
-   assert(pool.size == initial_size);
+   ASSERT(pool.size == initial_size);
 
    uint32_t padding;
    int32_t offset = anv_block_pool_alloc(&pool, block_size, &padding);
 
    /* Pool will have grown at least space to fit the new allocation. */
-   assert(pool.size > initial_size);
-   assert(pool.size >= initial_size + block_size);
+   ASSERT(pool.size > initial_size);
+   ASSERT(pool.size >= initial_size + block_size);
 
    /* The whole initial size is considered padding and the allocation should be
     * right next to it.
     */
-   assert(padding == initial_size);
-   assert(offset == initial_size);
+   ASSERT(padding == initial_size);
+   ASSERT(offset == initial_size);
 
    /* Use the memory to ensure it is valid. */
    void *map = anv_block_pool_map(&pool, offset, block_size);
diff --git a/src/intel/vulkan/tests/block_pool_no_free.c b/src/intel/vulkan/tests/block_pool_no_free.c
index b4b82693ae8..e0961ab3b9e 100644
--- a/src/intel/vulkan/tests/block_pool_no_free.c
+++ b/src/intel/vulkan/tests/block_pool_no_free.c
@@ -21,11 +21,10 @@
  * IN THE SOFTWARE.
  */
 
-#undef NDEBUG
-
 #include <pthread.h>
 
 #include "anv_private.h"
+#include "test_common.h"
 
 #define NUM_THREADS 16
 #define BLOCKS_PER_THREAD 1024
@@ -51,24 +50,24 @@ static void *alloc_blocks(void *_job)
       block = anv_block_pool_alloc(job->pool, block_size, NULL);
       data = anv_block_pool_map(job->pool, block, block_size);
       *data = block;
-      assert(block >= 0);
+      ASSERT(block >= 0);
       job->blocks[i] = block;
 
       block = anv_block_pool_alloc_back(job->pool, block_size);
       data = anv_block_pool_map(job->pool, block, block_size);
       *data = block;
-      assert(block < 0);
+      ASSERT(block < 0);
       job->back_blocks[i] = -block;
    }
 
    for (unsigned i = 0; i < BLOCKS_PER_THREAD; i++) {
       block = job->blocks[i];
       data = anv_block_pool_map(job->pool, block, block_size);
-      assert(*data == block);
+      ASSERT(*data == block);
 
       block = -job->back_blocks[i];
       data = anv_block_pool_map(job->pool, block, block_size);
-      assert(*data == block);
+      ASSERT(*data == block);
    }
 
    return NULL;
@@ -102,7 +101,7 @@ static void validate_monotonic(int32_t **blocks)
          break;
 
       /* That next element had better be higher than the previous highest */
-      assert(blocks[min_thread_idx][next[min_thread_idx]] > highest);
+      ASSERT(blocks[min_thread_idx][next[min_thread_idx]] > highest);
 
       highest = blocks[min_thread_idx][next[min_thread_idx]];
       next[min_thread_idx]++;
diff --git a/src/intel/vulkan/tests/state_pool.c b/src/intel/vulkan/tests/state_pool.c
index be844e0498f..5bdd1370090 100644
--- a/src/intel/vulkan/tests/state_pool.c
+++ b/src/intel/vulkan/tests/state_pool.c
@@ -21,11 +21,10 @@
  * IN THE SOFTWARE.
  */
 
-#undef NDEBUG
-
 #include <pthread.h>
 
 #include "anv_private.h"
+#include "test_common.h"
 
 #define NUM_THREADS 8
 #define STATES_PER_THREAD_LOG2 10
diff --git a/src/intel/vulkan/tests/state_pool_free_list_only.c b/src/intel/vulkan/tests/state_pool_free_list_only.c
index 05c3a3bb0c9..f35a4e98ec1 100644
--- a/src/intel/vulkan/tests/state_pool_free_list_only.c
+++ b/src/intel/vulkan/tests/state_pool_free_list_only.c
@@ -21,11 +21,10 @@
  * IN THE SOFTWARE.
  */
 
-#undef NDEBUG
-
 #include <pthread.h>
 
 #include "anv_private.h"
+#include "test_common.h"
 
 #define NUM_THREADS 8
 #define STATES_PER_THREAD_LOG2 12
@@ -55,7 +54,7 @@ int main(int argc, char **argv)
       struct anv_state states[NUM_THREADS * STATES_PER_THREAD];
       for (unsigned i = 0; i < NUM_THREADS * STATES_PER_THREAD; i++) {
          states[i] = anv_state_pool_alloc(&state_pool, 16, 16);
-         assert(states[i].offset != 0);
+         ASSERT(states[i].offset != 0);
       }
 
       for (unsigned i = 0; i < NUM_THREADS * STATES_PER_THREAD; i++)
diff --git a/src/intel/vulkan/tests/state_pool_no_free.c b/src/intel/vulkan/tests/state_pool_no_free.c
index 813b65be35d..91bfab8f4b1 100644
--- a/src/intel/vulkan/tests/state_pool_no_free.c
+++ b/src/intel/vulkan/tests/state_pool_no_free.c
@@ -21,11 +21,10 @@
  * IN THE SOFTWARE.
  */
 
-#undef NDEBUG
-
 #include <pthread.h>
 
 #include "anv_private.h"
+#include "test_common.h"
 
 #define NUM_THREADS 16
 #define STATES_PER_THREAD 1024
@@ -103,7 +102,7 @@ static void run_test()
          break;
 
       /* That next element had better be higher than the previous highest */
-      assert(jobs[max_thread_idx].offsets[next[max_thread_idx]] > highest);
+      ASSERT(jobs[max_thread_idx].offsets[next[max_thread_idx]] > highest);
 
       highest = jobs[max_thread_idx].offsets[next[max_thread_idx]];
       next[max_thread_idx]++;
diff --git a/src/intel/vulkan/tests/state_pool_padding.c b/src/intel/vulkan/tests/state_pool_padding.c
index bcd4c47fd69..596a400179f 100644
--- a/src/intel/vulkan/tests/state_pool_padding.c
+++ b/src/intel/vulkan/tests/state_pool_padding.c
@@ -21,9 +21,8 @@
  * IN THE SOFTWARE.
  */
 
-#undef NDEBUG
-
 #include "anv_private.h"
+#include "test_common.h"
 
 int main(int argc, char **argv)
 {
@@ -50,30 +49,30 @@ int main(int argc, char **argv)
    struct anv_state state = anv_state_pool_alloc(&state_pool, pool_size, 16);
 
    /* The pool must have grown */
-   assert(bp->size > pool_size);
+   ASSERT(bp->size > pool_size);
 
    /* And the state must have been allocated at the end of the original size  */
-   assert(state.offset == pool_size);
+   ASSERT(state.offset == pool_size);
 
    /* A new allocation that fits into the returned empty space should have an
     * offset within the original pool size
     */
    state = anv_state_pool_alloc(&state_pool, 4096, 16);
-   assert(state.offset + state.alloc_size <= pool_size);
+   ASSERT(state.offset + state.alloc_size <= pool_size);
 
    /* We should be able to allocate pool->block_size'd chunks in the returned area
     */
    int left_chunks = pool_size / 4096 - 2;
    for (int i = 0; i < left_chunks; i++) {
       state = anv_state_pool_alloc(&state_pool, 4096, 16);
-      assert(state.offset + state.alloc_size <= pool_size);
+      ASSERT(state.offset + state.alloc_size <= pool_size);
    }
 
    /* Now the next chunk to be allocated should make the pool grow again */
    pool_size = bp->size;
    state = anv_state_pool_alloc(&state_pool, 4096, 16);
-   assert(bp->size > pool_size);
-   assert(state.offset == pool_size);
+   ASSERT(bp->size > pool_size);
+   ASSERT(state.offset == pool_size);
 
    anv_state_pool_finish(&state_pool);
 }
diff --git a/src/intel/vulkan/tests/state_pool_test_helper.h b/src/intel/vulkan/tests/state_pool_test_helper.h
index 0e56431303f..f22a28ecc6f 100644
--- a/src/intel/vulkan/tests/state_pool_test_helper.h
+++ b/src/intel/vulkan/tests/state_pool_test_helper.h
@@ -46,7 +46,7 @@ static void *alloc_states(void *void_job)
       for (unsigned i = 0; i < chunk_size; i++) {
          states[i] = anv_state_pool_alloc(job->pool, 16, 16);
          memset(states[i].map, 139, 16);
-         assert(states[i].offset != 0);
+         ASSERT(states[i].offset != 0);
       }
 
       for (unsigned i = 0; i < chunk_size; i++)
diff --git a/src/intel/vulkan/tests/test_common.h b/src/intel/vulkan/tests/test_common.h
new file mode 100644
index 00000000000..3f883e3bdcd
--- /dev/null
+++ b/src/intel/vulkan/tests/test_common.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright © 2020 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.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define ASSERT(cond)                                                    \
+   do {                                                                 \
+      if (!(cond)) {                                                    \
+         fprintf(stderr, "%s:%d: Test assertion `%s` failed.\n",        \
+                 __FILE__, __LINE__, # cond);                           \
+         abort();                                                       \
+      }                                                                 \
+   } while (false)



More information about the mesa-commit mailing list