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: f7c56475d2 ("anv/tests: compile to something sensible in release builds")
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4994>
(cherry picked from commit f4638cfdad)
This commit is contained in:
Ian Romanick 2020-05-11 12:33:24 -07:00 committed by Dylan Baker
parent 3705ec33a5
commit fcc8debd5a
9 changed files with 60 additions and 32 deletions

View file

@ -31,7 +31,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"
},

View file

@ -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);

View file

@ -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]++;

View file

@ -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

View file

@ -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++)

View file

@ -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]++;

View file

@ -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);
}

View file

@ -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++)

View file

@ -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)