1 /*
2   MBTOWC: int mbtowc (wchar_t *wc, char *s, size_t n)
3 */
4 
5 #define TST_FUNCTION mbtowc
6 
7 #include "tsp_common.c"
8 #include "dat_mbtowc.c"
9 
10 
11 int
tst_mbtowc(FILE * fp,int debug_flg)12 tst_mbtowc (FILE * fp, int debug_flg)
13 {
14   TST_DECL_VARS (int);
15   char w_flg, s_flg;
16   const char *s_in;
17   size_t n;
18   wchar_t wc, wc_ex, *wp;
19 
20   TST_DO_TEST (mbtowc)
21   {
22     TST_HEAD_LOCALE (mbtowc, S_MBTOWC);
23     TST_DO_REC (mbtowc)
24     {
25       if (mbstowcs (NULL, "", 0) != 0)
26 	{
27 	  err_count++;
28 	  Result (C_FAILURE, S_MBSTOWCS, CASE_3,
29 		  "Initialization failed - skipping this test case.");
30 	  continue;
31 	}
32 
33       TST_DO_SEQ (MBTOWC_SEQNUM)
34       {
35 	TST_GET_ERRET_SEQ (mbtowc);
36 	w_flg = TST_INPUT_SEQ (mbtowc).w_flg;
37 	s_flg = TST_INPUT_SEQ (mbtowc).s_flg;
38 	n = TST_INPUT_SEQ (mbtowc).n;
39 
40 	if (n == USE_MBCURMAX)
41 	  {
42 	    n = MB_CUR_MAX;
43 	  }
44 
45 	if (s_flg == 0)
46 	  s_in = NULL;
47 	else
48 	  s_in = TST_INPUT_SEQ (mbtowc).s;
49 
50 	wp = (wchar_t *) ((w_flg == 0) ? NULL : &wc);
51 
52 	/* XXX Clear the internal state.  We should probably have
53 	   a flag for this.  */
54 	mbtowc (NULL, NULL, 0);
55 
56 	TST_CLEAR_ERRNO;
57 	ret = mbtowc (wp, s_in, n);
58 	TST_SAVE_ERRNO;
59 
60 	if (debug_flg)
61 	  {
62 	    fprintf (stdout, "mbtowc() [ %s : %d ] ret = %d\n", locale,
63 		     rec + 1, ret);
64 	    fprintf (stdout, "			   errno      = %d\n",
65 		     errno_save);
66 	  }
67 
68 	TST_IF_RETURN (S_MBTOWC)
69 	{
70 	  if (s_in == NULL)
71 	    {			/* state dependency */
72 	      if (ret_exp == +1)
73 		{		/* state-dependent  */
74 		  if (ret != 0)
75 		    {
76 		      /* Non-zero: state-dependent encoding.  */
77 		      Result (C_SUCCESS, S_MBTOWC, CASE_3, MS_PASSED);
78 		    }
79 		  else
80 		    {
81 		      err_count++;
82 		      Result (C_FAILURE, S_MBTOWC, CASE_3,
83 			      "should be state-dependent encoding, "
84 			      "but a return value shows it is "
85 			      "state-independent");
86 		    }
87 		}
88 
89 	      if (ret_exp == 0)
90 		{		/* state-independent */
91 		  if (ret == 0)
92 		    {
93 		      /* Non-zero: state-dependent encoding.  */
94 		      Result (C_SUCCESS, S_MBTOWC, CASE_3, MS_PASSED);
95 		    }
96 		  else
97 		    {
98 		      err_count++;
99 		      Result (C_FAILURE, S_MBTOWC, CASE_3,
100 			      "should be state-independent encoding, "
101 			      "but a return value shows it is "
102 			      "state-dependent");
103 		    }
104 		}
105 	    }
106 	}
107 
108 	if ((wp == NULL || s_in == NULL || s_in[0] == 0) || ret <= 0)
109 	  {
110 	    continue;
111 	  }
112 
113 	wc_ex = TST_EXPECT_SEQ (mbtowc).wc;
114 
115 	if (wc_ex == wc)
116 	  {
117 	    Result (C_SUCCESS, S_MBTOWC, CASE_4, MS_PASSED);
118 	  }
119 	else
120 	  {
121 	    err_count++;
122 	    Result (C_FAILURE, S_MBTOWC, CASE_4,
123 		    "converted wc is different from an expected wc");
124 	  }
125       }
126     }
127   }
128 
129   return err_count;
130 }
131