lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* |
| 2 | WCSTOMBS: size_t wcstombs (char *s, const wchar_t *ws, size_t n) |
| 3 | */ |
| 4 | |
| 5 | #define TST_FUNCTION wcstombs |
| 6 | |
| 7 | #include "tsp_common.c" |
| 8 | #include "dat_wcstombs.c" |
| 9 | |
| 10 | #define MARK_VAL 0x01 |
| 11 | |
| 12 | int |
| 13 | tst_wcstombs (FILE * fp, int debug_flg) |
| 14 | { |
| 15 | TST_DECL_VARS (size_t); |
| 16 | char s_flg, n; |
| 17 | wchar_t *ws; |
| 18 | char s[MBSSIZE], *s_in; |
| 19 | int err, i; |
| 20 | char *s_ex; |
| 21 | |
| 22 | TST_DO_TEST (wcstombs) |
| 23 | { |
| 24 | TST_HEAD_LOCALE (wcstombs, S_WCSTOMBS); |
| 25 | TST_DO_REC (wcstombs) |
| 26 | { |
| 27 | TST_GET_ERRET (wcstombs); |
| 28 | memset (s, MARK_VAL, MBSSIZE); |
| 29 | |
| 30 | s_flg = TST_INPUT (wcstombs).s_flg; |
| 31 | s_in = (s_flg == 1) ? s : (char *) NULL; |
| 32 | ws = TST_INPUT (wcstombs).ws; |
| 33 | n = TST_INPUT (wcstombs).n; |
| 34 | |
| 35 | TST_CLEAR_ERRNO; |
| 36 | ret = wcstombs (s_in, ws, n); |
| 37 | TST_SAVE_ERRNO; |
| 38 | |
| 39 | if (debug_flg) |
| 40 | { |
| 41 | fprintf (stdout, "wcstombs: ret = %zu\n", ret); |
| 42 | } |
| 43 | |
| 44 | TST_IF_RETURN (S_WCSTOMBS) |
| 45 | { |
| 46 | }; |
| 47 | |
| 48 | if (s_in != NULL && ret != (size_t) - 1) |
| 49 | { |
| 50 | /* No definition for s, when error occurs. */ |
| 51 | s_ex = TST_EXPECT (wcstombs).s; |
| 52 | |
| 53 | for (err = 0, i = 0; i <= ret && i < MBSSIZE; i++) |
| 54 | { |
| 55 | if (debug_flg) |
| 56 | { |
| 57 | fprintf (stdout, |
| 58 | " : s[%d] = 0x%hx <-> 0x%hx = s_ex[%d]\n", i, |
| 59 | s[i], s_ex[i], i); |
| 60 | } |
| 61 | |
| 62 | if (i == ret && ret == n) /* no null termination */ |
| 63 | { |
| 64 | if (s[i] == MARK_VAL) |
| 65 | { |
| 66 | Result (C_SUCCESS, S_WCSTOMBS, CASE_4, MS_PASSED); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | err_count++; |
| 71 | Result (C_FAILURE, S_WCSTOMBS, CASE_4, |
| 72 | "should not be null terminated " |
| 73 | "(it may be a null char), but it is"); |
| 74 | } |
| 75 | |
| 76 | break; |
| 77 | } |
| 78 | |
| 79 | if (i == ret && ret < n) /* null termination */ |
| 80 | { |
| 81 | if (s[i] == 0) |
| 82 | { |
| 83 | Result (C_SUCCESS, S_WCSTOMBS, CASE_5, MS_PASSED); |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | err_count++; |
| 88 | Result (C_FAILURE, S_WCSTOMBS, CASE_5, |
| 89 | "should be null terminated, but it is not"); |
| 90 | } |
| 91 | |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | if (s[i] != s_ex[i]) |
| 96 | { |
| 97 | err++; |
| 98 | err_count++; |
| 99 | Result (C_FAILURE, S_WCSTOMBS, CASE_6, |
| 100 | "converted string is different from an " |
| 101 | "expected string"); |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (!err) |
| 107 | { |
| 108 | Result (C_SUCCESS, S_WCSTOMBS, CASE_6, MS_PASSED); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return err_count; |
| 115 | } |