Update string tests
This commit is contained in:
		
							parent
							
								
									25bb390408
								
							
						
					
					
						commit
						0e0346c368
					
				@ -3,6 +3,59 @@
 | 
			
		||||
 | 
			
		||||
#include "plug.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static vtype_string string_random(unsigned int n) {
 | 
			
		||||
    vtype_string x;
 | 
			
		||||
 | 
			
		||||
    if (random_boolean()) {
 | 
			
		||||
        x.buffer = random_utf8_cstring(n);
 | 
			
		||||
    } else x.buffer = random_ascii_cstring(n);
 | 
			
		||||
 | 
			
		||||
    return x;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void string_concat_random(vtype_string* x, unsigned int n) {
 | 
			
		||||
    char* v;
 | 
			
		||||
 | 
			
		||||
    if (random_boolean()) {
 | 
			
		||||
        v = random_utf8_cstring(n);
 | 
			
		||||
    } else v = random_ascii_cstring(n);
 | 
			
		||||
 | 
			
		||||
    string_concat(x, v);
 | 
			
		||||
    free(v);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void string_info(vtype_string* x) {
 | 
			
		||||
    printf("\e[36mString consists of \e[m\e[32m%lu\e[m\e[36m utf8 chars (\e[m\e[32m%lu bytes\e[m\e[36m)\e[m\n", string_size(x), string_nmemb(x));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void string_print(const vtype_string* x, const char* prefix) {
 | 
			
		||||
    if (!prefix) puts("\e[36mString content:\e[m\n");
 | 
			
		||||
    else printf("\e[36mString %s content:\e[m\n\n", prefix);
 | 
			
		||||
    printf("\e[33m%s\e[m\n", x->buffer);
 | 
			
		||||
    put_separator();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int main(int argc, char** argv) {
 | 
			
		||||
    test_init(argc, argv);
 | 
			
		||||
    vtype_string x;
 | 
			
		||||
 | 
			
		||||
    x = string_random(31);
 | 
			
		||||
    string_print(&x, "(part 1)");
 | 
			
		||||
 | 
			
		||||
    string_concat(&x, '\n');
 | 
			
		||||
    string_concat_random(&x, 31);
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        void* hack = string_at(&x, 32);
 | 
			
		||||
        string_print((void*)&hack, "(part 2)");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    string_info(&x);
 | 
			
		||||
    string_print(&x, "concatenated");
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    string_free(&x);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,8 @@
 | 
			
		||||
/* This software is licensed by the MIT License, see LICENSE file */
 | 
			
		||||
/*                                Copyright © 2022 Gregory Lirent */
 | 
			
		||||
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include "../../../modules/libunic/include.h"
 | 
			
		||||
#include "../../../include/extra/string.h"
 | 
			
		||||
 | 
			
		||||
#include "../../include/random.h"
 | 
			
		||||
@ -21,3 +23,27 @@ int array_compare (const vtype_array*  s0, const vtype_array*  s1) { return rand
 | 
			
		||||
int list_compare  (const vtype_list*   s0, const vtype_list*   s1) { return random_int8(); }
 | 
			
		||||
int map_compare   (const vtype_map*    s0, const vtype_map*    s1) { return random_int8(); }
 | 
			
		||||
int vset_compare  (const vtype_set*    s0, const vtype_set*    s1) { return random_int8(); }
 | 
			
		||||
 | 
			
		||||
static char* random_ascii_cstring(size_t size) {
 | 
			
		||||
    char* v = malloc(size + 1);
 | 
			
		||||
    char* p = v;
 | 
			
		||||
 | 
			
		||||
    while (size--) {
 | 
			
		||||
        *(p++) = random_ascii_char();
 | 
			
		||||
    }
 | 
			
		||||
    *p = 0;
 | 
			
		||||
 | 
			
		||||
    return v;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static char* random_utf8_cstring(size_t size) {
 | 
			
		||||
    char* v = malloc(size * 4 + 1);
 | 
			
		||||
    char* p = v;
 | 
			
		||||
 | 
			
		||||
    while (size--) {
 | 
			
		||||
        p = tochar_unicode(p, random_unicode_symbol());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    *p = 0;
 | 
			
		||||
    return v;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user