setos

拙OS
Log | Files | Refs | LICENSE

utils_test.c (1505B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #include "../uefi.h"
      6 #include "../utils.h"
      7 
      8 EFI_SYSTEM_TABLE *SystemTable;
      9 
     10 int
     11 str16cmp(CHAR16 *s, CHAR16 *t)
     12 {
     13 	for (; *s != 0 && *t != 0; s++, t++) {
     14 		if (*s == *t) {
     15 			continue;
     16 		} else if (*s < *t) {
     17 			return -1;
     18 		} else {
     19 			return 1;
     20 		}
     21 	}
     22 	if (*s != 0) {
     23 		return 1;
     24 	} else if (*t != 0) {
     25 		return -1;
     26 	} else {
     27 		return 0;
     28 	}
     29 }
     30 
     31 // Strtostr16 converts string s to UTF16 string.
     32 // Caller should free the returned pointer.
     33 CHAR16 *
     34 strtostr16(char *s)
     35 {
     36 	int n;
     37 	CHAR16 *ss, *st;
     38 
     39 	n = strlen(s);
     40 	ss = st = (CHAR16 *) malloc((n+1) * sizeof(CHAR16));
     41 	if (!ss)
     42 		return NULL;
     43 	for (; *s; ) {
     44 		*ss++ = (CHAR16) *s++;
     45 	}
     46 	return st;
     47 }
     48 
     49 char *
     50 str16tostr(CHAR16 *s)
     51 {
     52 	int n = 0;
     53 	CHAR16 *t;
     54 	for (t = s; *t; t++) {
     55 		n++;
     56 	}
     57 	char *ss, *tt;
     58 	ss = tt = (char *)malloc((n + 1) * sizeof(char));
     59 	if (!ss) {
     60 		return NULL;
     61 	}
     62 	for (tt = ss, t = s; *t;) {
     63 		*tt++ = (char) (*t++ & 0xff);
     64 	}
     65 	*tt = '\0';
     66 	return ss;
     67 }
     68 
     69 int
     70 testSprinth()
     71 {
     72 	int nerr = 0;
     73 	struct test {
     74 		UINT64 input;
     75 		CHAR16 *want;
     76 	} tests[] = {
     77 		{0xdeadbeef, strtostr16("0x00000000deadbeef")},
     78 		{0xcafecafe, strtostr16("0x00000000cafecafe")},
     79 		{0, NULL},
     80 	};
     81 	struct test *t;
     82 	CHAR16 got[19];
     83 	for (t = tests; t->want; t++) {
     84 		if (str16cmp(t->want, sprinth(t->input, got)) != 0) {
     85 			fprintf(stderr, "sprinth(0x%x, got) = %s, want: %s\n",
     86 				t->input, str16tostr(got), str16tostr(t->want));
     87 			nerr++;
     88 		}
     89 	}
     90 	return nerr;
     91 }
     92 
     93 int
     94 main(void)
     95 {
     96 	testSprinth();
     97 }