mirror of
				https://gitlab.freedesktop.org/pipewire/pipewire.git
				synced 2025-11-03 09:01:54 -05:00 
			
		
		
		
	tests: add array test
This commit is contained in:
		
							parent
							
								
									e23deae087
								
							
						
					
					
						commit
						fb5ae6df43
					
				
					 3 changed files with 107 additions and 2 deletions
				
			
		| 
						 | 
				
			
			@ -1,3 +1,8 @@
 | 
			
		|||
test_array = executable('test-array', 'test-array.c',
 | 
			
		||||
                        dependencies : [pipewire_dep],
 | 
			
		||||
                        install : false)
 | 
			
		||||
test('test-array', test_array)
 | 
			
		||||
 | 
			
		||||
if have_cpp
 | 
			
		||||
test_cpp = executable('test-cpp', 'test-cpp.cpp',
 | 
			
		||||
                        dependencies : [pipewire_dep],
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										100
									
								
								src/tests/test-array.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										100
									
								
								src/tests/test-array.c
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,100 @@
 | 
			
		|||
/* PipeWire
 | 
			
		||||
 * Copyright (C) 2019 Wim Taymans <wim.taymans@gmail.com>
 | 
			
		||||
 *
 | 
			
		||||
 * This library is free software; you can redistribute it and/or
 | 
			
		||||
 * modify it under the terms of the GNU Library General Public
 | 
			
		||||
 * License as published by the Free Software Foundation; either
 | 
			
		||||
 * version 2 of the License, or (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This library is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 | 
			
		||||
 * Library General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU Library General Public
 | 
			
		||||
 * License along with this library; if not, write to the
 | 
			
		||||
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 | 
			
		||||
 * Boston, MA 02110-1301, USA.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <pipewire/array.h>
 | 
			
		||||
 | 
			
		||||
static void test_abi(void)
 | 
			
		||||
{
 | 
			
		||||
	/* array */
 | 
			
		||||
	spa_assert(sizeof(struct pw_array) == 32);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void test_array(void)
 | 
			
		||||
{
 | 
			
		||||
	struct pw_array arr;
 | 
			
		||||
	uint32_t *ptr;
 | 
			
		||||
	uint32_t vals[] = { 0, 100, 0x8a, 0 };
 | 
			
		||||
	size_t i;
 | 
			
		||||
 | 
			
		||||
	pw_array_init(&arr, 64);
 | 
			
		||||
	spa_assert(SPA_N_ELEMENTS(vals) == 4);
 | 
			
		||||
 | 
			
		||||
	spa_assert(pw_array_get_len(&arr, uint32_t) == 0);
 | 
			
		||||
	spa_assert(pw_array_check_index(&arr, 0, uint32_t) == false);
 | 
			
		||||
	spa_assert(pw_array_first(&arr) == pw_array_end(&arr));
 | 
			
		||||
	pw_array_for_each(ptr, &arr)
 | 
			
		||||
		spa_assert_not_reached();
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < 4; i++) {
 | 
			
		||||
		ptr = (uint32_t*)pw_array_add(&arr, sizeof(uint32_t));
 | 
			
		||||
		*ptr = vals[i];
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	spa_assert(pw_array_get_len(&arr, uint32_t) == 4);
 | 
			
		||||
	spa_assert(pw_array_check_index(&arr, 2, uint32_t) == true);
 | 
			
		||||
	spa_assert(pw_array_check_index(&arr, 3, uint32_t) == true);
 | 
			
		||||
	spa_assert(pw_array_check_index(&arr, 4, uint32_t) == false);
 | 
			
		||||
 | 
			
		||||
	i = 0;
 | 
			
		||||
	pw_array_for_each(ptr, &arr) {
 | 
			
		||||
		spa_assert(*ptr == vals[i++]);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* remove second */
 | 
			
		||||
	ptr = pw_array_get_unchecked(&arr, 2, uint32_t);
 | 
			
		||||
	spa_assert(ptr != NULL);
 | 
			
		||||
	pw_array_remove(&arr, ptr);
 | 
			
		||||
	spa_assert(pw_array_get_len(&arr, uint32_t) == 3);
 | 
			
		||||
	spa_assert(pw_array_check_index(&arr, 3, uint32_t) == false);
 | 
			
		||||
	ptr = pw_array_get_unchecked(&arr, 2, uint32_t);
 | 
			
		||||
	spa_assert(ptr != NULL);
 | 
			
		||||
	spa_assert(*ptr == vals[3]);
 | 
			
		||||
 | 
			
		||||
	/* remove first */
 | 
			
		||||
	ptr = pw_array_get_unchecked(&arr, 0, uint32_t);
 | 
			
		||||
	spa_assert(ptr != NULL);
 | 
			
		||||
	pw_array_remove(&arr, ptr);
 | 
			
		||||
	spa_assert(pw_array_get_len(&arr, uint32_t) == 2);
 | 
			
		||||
	ptr = pw_array_get_unchecked(&arr, 0, uint32_t);
 | 
			
		||||
	spa_assert(ptr != NULL);
 | 
			
		||||
	spa_assert(*ptr == vals[1]);
 | 
			
		||||
 | 
			
		||||
	/* iterate */
 | 
			
		||||
	ptr = (uint32_t*)pw_array_first(&arr);
 | 
			
		||||
	spa_assert(pw_array_check(&arr, ptr));
 | 
			
		||||
	spa_assert(*ptr == vals[1]);
 | 
			
		||||
	ptr++;
 | 
			
		||||
	spa_assert(pw_array_check(&arr, ptr));
 | 
			
		||||
	spa_assert(*ptr == vals[3]);
 | 
			
		||||
	ptr++;
 | 
			
		||||
	spa_assert(pw_array_check(&arr, ptr) == false);
 | 
			
		||||
 | 
			
		||||
	pw_array_reset(&arr);
 | 
			
		||||
	spa_assert(pw_array_get_len(&arr, uint32_t) == 0);
 | 
			
		||||
 | 
			
		||||
	pw_array_clear(&arr);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(int argc, char *argv[])
 | 
			
		||||
{
 | 
			
		||||
	test_abi();
 | 
			
		||||
	test_array();
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue