Update array test
This commit is contained in:
		
							parent
							
								
									ee602d91b9
								
							
						
					
					
						commit
						f2742661fb
					
				| @ -1,8 +1,102 @@ | ||||
| /* This software is licensed by the MIT License, see LICENSE file */ | ||||
| /*                                Copyright © 2022 Gregory Lirent */ | ||||
| 
 | ||||
| #include "../../../include/extra/vtype.h" | ||||
| #include "plug.h" | ||||
| 
 | ||||
| static void array_push_random_value(vtype_array* x) { | ||||
|     switch (random_uint8()%10) { | ||||
|         default: | ||||
|         case 0: array_push(x, random_boolean()); break; | ||||
|         case 1: array_push(x, random_uint8());   break; | ||||
|         case 2: array_push(x, random_uint16());  break; | ||||
|         case 3: array_push(x, random_uint32());  break; | ||||
|         case 4: array_push(x, random_uint64());  break; | ||||
|         case 5: array_push(x, random_int8());    break; | ||||
|         case 6: array_push(x, random_int16());   break; | ||||
|         case 7: array_push(x, random_int32());   break; | ||||
|         case 8: array_push(x, random_int64());   break; | ||||
|         case 9: array_push(x, (void*)((sizeof(void*) == 8) ? random_uint64() : random_uint32())); break; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| static void array_push_random_float(vtype_array* x) { | ||||
|     switch (random_uint8()%3) { | ||||
|         default: | ||||
|         case 0: array_push(x, random_float());   break; | ||||
|         case 1: array_push(x, random_double());  break; | ||||
|         case 2: array_push(x, random_ldouble()); break; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| static void array_push_random(vtype_array* x) { | ||||
|     if (x->type < VTYPE_FLOAT) { | ||||
|         array_push_random_value(x); | ||||
|     } else array_push_random_float(x); | ||||
| } | ||||
| 
 | ||||
| static void array_init_random(vtype_array* x) { | ||||
|     switch (random_uint8()%13) { | ||||
|         default: | ||||
|         case  0: array_init(x, VTYPE_BOOLEAN); break; | ||||
|         case  1: array_init(x, VTYPE_UINT8);   break; | ||||
|         case  2: array_init(x, VTYPE_UINT16);  break; | ||||
|         case  3: array_init(x, VTYPE_UINT32);  break; | ||||
|         case  4: array_init(x, VTYPE_UINT64);  break; | ||||
|         case  5: array_init(x, VTYPE_INT8);    break; | ||||
|         case  6: array_init(x, VTYPE_INT16);   break; | ||||
|         case  7: array_init(x, VTYPE_INT32);   break; | ||||
|         case  8: array_init(x, VTYPE_INT64);   break; | ||||
|         case  9: array_init(x, VTYPE_POINTER); break; | ||||
|         case 10: array_init(x, VTYPE_FLOAT);   break; | ||||
|         case 11: array_init(x, VTYPE_DOUBLE);  break; | ||||
|         case 12: array_init(x, VTYPE_LDOUBLE); break; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| static vtype_array array_random() { | ||||
|     unsigned short n = random_int16(); | ||||
| 
 | ||||
|     vtype_array x; | ||||
| 
 | ||||
|     array_init_random(&x); | ||||
| 
 | ||||
|     for (int i = 0; i < 10; ++i) { | ||||
|         array_push_random(&x); | ||||
|     } | ||||
| 
 | ||||
|     return x; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| int main(int argc, char** argv) { | ||||
|     test_init(argc, argv); | ||||
| 
 | ||||
|     vtype_array x = { .mem = 0 }; | ||||
| 
 | ||||
|     do { | ||||
|         array_free(&x); | ||||
|         x = array_random(); | ||||
|     } while (x.type >= VTYPE_FLOAT); | ||||
| 
 | ||||
|     printf("Array initialized with type `%s`;\n", libcdsb_vtype_name(x.type)); | ||||
|     printf("Array has %lu elements (%lu bytes);\n", array_size(&x), array_nmemb(&x)); | ||||
|     puts(""); | ||||
| 
 | ||||
|     for (int i = 0; i < x.size; ++i) { | ||||
|         printf("%s\n", libcdsb_vtype_stringify(array_at(&x, i), x.type)); | ||||
|     } | ||||
| 
 | ||||
|     for (vtype_uint64 v = 0; v < UINT64_MAX; ++v) { | ||||
|         ssize_t index = array_indexof(&x, v); | ||||
|         if (index >= 0) { | ||||
|             printf("Value %lu was found on the position %ld\n", v, index); | ||||
|             break; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|     array_free(&x); | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user