55#include "gitoid.h"
66
77#define LEN (arr ) (sizeof(arr) / sizeof(arr[0]));
8+ #define TEST (NAME ) {.name = #NAME, .fn = NAME}
89
910void test_gitoid_new_from_str () {
1011 GitOid gitoid = gitoid_new_from_str (HashAlgorithm_Sha1 , ObjectType_Blob , "hello world" );
12+ assert (!gitoid_invalid (& gitoid ));
1113 assert (gitoid .len == 20 );
1214 assert (gitoid .value [0 ] == 149 );
1315}
1416
1517void test_gitoid_new_from_bytes () {
16- // Section that creates the byte array was heavily inspired by [1].
17- //
18- // Does not do error checking, and is intended solely for test purposes.
19- // The length of `byte_array` is equal to the length of `string` plus one,
20- // to make space for the nul-terminator.
21- //
22- // [1]: https://stackoverflow.com/a/3409211/2308264
23- const char * string = "hello_world" ;
24- const char * position = string ;
25- unsigned char byte_array [12 ];
26- size_t size = LEN (byte_array );
27- for (size_t count = 0 ; count < size ; ++ count ) {
28- sscanf (position , "%2hhx" , & byte_array [count ]);
29- position += 2 ;
30- }
31- uint8_t byte_array_length = sizeof byte_array ;
18+ unsigned char bytes [] = {0x00 , 0x01 , 0x02 , 0x03 ,
19+ 0x04 , 0x05 , 0x06 , 0x07 ,
20+ 0x08 , 0x09 , 0x0A , 0x0B ,
21+ 0x0C , 0x0D , 0x0E , 0x0F };
22+ uint8_t bytes_len = LEN (bytes );
3223
3324 GitOid gitoid = gitoid_new_from_bytes (
3425 HashAlgorithm_Sha1 ,
3526 ObjectType_Blob ,
36- & byte_array_length ,
37- * byte_array
27+ bytes ,
28+ bytes_len
3829 );
3930
31+ assert (!gitoid_invalid (& gitoid ));
4032 assert (gitoid .len == 20 );
41- assert (gitoid .value [0 ] == 130 );
33+ assert (gitoid .value [0 ] == 182 );
4234}
4335
4436void test_gitoid_new_from_url () {
4537 char * url = "gitoid:blob:sha256:fee53a18d32820613c0527aa79be5cb30173c823a9b448fa4817767cc84c6f03" ;
4638 GitOid gitoid = gitoid_new_from_url (url );
39+ assert (!gitoid_invalid (& gitoid ));
4740 assert (gitoid .len == 32 );
4841 assert (gitoid .value [0 ] == 254 );
4942}
5043
5144void test_gitoid_get_url () {
5245 char * url_in = "gitoid:blob:sha256:fee53a18d32820613c0527aa79be5cb30173c823a9b448fa4817767cc84c6f03" ;
5346 GitOid gitoid = gitoid_new_from_url (url_in );
47+ assert (!gitoid_invalid (& gitoid ));
5448 char * url_out = gitoid_get_url (& gitoid );
5549 assert (strncmp (url_in , url_out , 83 ) == 0 );
5650 gitoid_str_free (url_out );
5751}
5852
5953void test_gitoid_hash_algorithm_name () {
6054 GitOid gitoid = gitoid_new_from_str (HashAlgorithm_Sha1 , ObjectType_Blob , "hello world" );
55+ assert (!gitoid_invalid (& gitoid ));
6156 const char * hash_algorithm = gitoid_hash_algorithm_name (gitoid .hash_algorithm );
6257 assert (strncmp (hash_algorithm , "sha1" , 4 ) == 0 );
6358}
6459
6560void test_gitoid_object_type_name () {
6661 GitOid gitoid = gitoid_new_from_str (HashAlgorithm_Sha1 , ObjectType_Blob , "hello world" );
62+ assert (!gitoid_invalid (& gitoid ));
6763 const char * object_type = gitoid_object_type_name (gitoid .object_type );
6864 assert (strncmp (object_type , "blob" , 4 ) == 0 );
6965}
7066
7167void test_gitoid_validity () {
72- // Notice the SHA type is invalid.
7368 char * validity_url = "gitoid:blob:sha000:fee53a18d32820613c0527aa79be5cb30173c823a9b448fa4817767cc84c6f03" ;
7469 GitOid gitoid = gitoid_new_from_url (validity_url );
7570 assert (gitoid_invalid (& gitoid ));
7671
77- // Also test the error message reporting.
7872 char * expected_msg = "string is not a valid GitOID URL" ;
7973 char error_msg [256 ];
8074 gitoid_get_error_message (error_msg , 256 );
@@ -91,14 +85,14 @@ typedef struct test {
9185int main () {
9286 setvbuf (stdout , NULL , _IONBF , 0 );
9387
94- test_t tests [7 ] = {
95- {. name = "gitoid_new_from_str" , . fn = test_gitoid_new_from_str } ,
96- {. name = "gitoid_new_from_bytes" , . fn = test_gitoid_new_from_bytes } ,
97- {. name = "gitoid_new_from_url" , . fn = test_gitoid_new_from_url } ,
98- {. name = "gitoid_get_url" , . fn = test_gitoid_get_url } ,
99- {. name = "gitoid_hash_algorithm_name" , . fn = test_gitoid_hash_algorithm_name } ,
100- {. name = "gitoid_object_type_name" , . fn = test_gitoid_object_type_name } ,
101- {. name = "gitoid_validity" , . fn = test_gitoid_validity } ,
88+ test_t tests [] = {
89+ TEST ( test_gitoid_new_from_str ) ,
90+ TEST ( test_gitoid_new_from_bytes ) ,
91+ TEST ( test_gitoid_new_from_url ) ,
92+ TEST ( test_gitoid_get_url ) ,
93+ TEST ( test_gitoid_hash_algorithm_name ) ,
94+ TEST ( test_gitoid_object_type_name ) ,
95+ TEST ( test_gitoid_validity ) ,
10296 };
10397
10498 size_t n_tests = LEN (tests );
0 commit comments