1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using Xunit;
6 
7 namespace System.Text.Tests
8 {
9     // GetCharCount(System.Byte[],System.Int32,System.Int32,System.Boolean)
10     public class DecoderGetCharCount2
11     {
12         #region Private Fields
13         private const int c_SIZE_OF_ARRAY = 127;
14         private readonly RandomDataGenerator _generator = new RandomDataGenerator();
15         #endregion
16 
17         #region Positive Test Cases
18         // PosTest1: Call GetCharCount with ASCII decoder and ASCII byte array
19         [Fact]
PosTest1()20         public void PosTest1()
21         {
22             Decoder decoder = Encoding.UTF8.GetDecoder();
23             byte[] bytes = new byte[c_SIZE_OF_ARRAY];
24             int expected = bytes.Length;
25             for (int i = 0; i < bytes.Length; ++i)
26             {
27                 bytes[i] = (byte)i;
28             }
29 
30             VerificationHelper(decoder, bytes, 0, bytes.Length, true, expected, "001.1");
31             VerificationHelper(decoder, bytes, 0, bytes.Length, false, expected, "001.2");
32         }
33 
34         // PosTest2: Call GetCharCount with Unicode decoder and ASCII byte array
35         [Fact]
PosTest2()36         public void PosTest2()
37         {
38             Decoder decoder = Encoding.Unicode.GetDecoder();
39             byte[] bytes = new byte[c_SIZE_OF_ARRAY];
40             for (int i = 0; i < bytes.Length; ++i)
41             {
42                 bytes[i] = (byte)i;
43             }
44 
45             VerificationHelper(decoder, bytes, 0, bytes.Length, true, bytes.Length / 2 + 1, "002.1");
46             VerificationHelper(decoder, bytes, 0, bytes.Length, false, bytes.Length / 2, "002.2");
47         }
48 
49         // PosTest3: Call GetCharCount with Unicode decoder and Unicode byte array
50         [Fact]
PosTest3()51         public void PosTest3()
52         {
53             Decoder decoder = Encoding.Unicode.GetDecoder();
54             int expected = 6;
55             // Unicode string: 这个一个测试
56             byte[] bytes = new byte[] {
57                 217,
58                 143,
59                 42,
60                 78,
61                 0,
62                 78,
63                 42,
64                 78,
65                 75,
66                 109,
67                 213,
68                 139
69             };
70 
71             VerificationHelper(decoder, bytes, 0, bytes.Length, true, expected, "003.1");
72             VerificationHelper(decoder, bytes, 0, bytes.Length, false, expected, "003.2");
73         }
74 
75         // PosTest4: Call GetCharCount with Unicode decoder and Arbitrary byte array
76         [Fact]
PosTest4()77         public void PosTest4()
78         {
79             Decoder decoder = Encoding.Unicode.GetDecoder();
80             byte[] bytes = new byte[c_SIZE_OF_ARRAY];
81             _generator.GetBytes(-55, bytes);
82 
83             decoder.GetCharCount(bytes, 0, bytes.Length, true);
84             decoder.GetCharCount(bytes, 0, bytes.Length, false);
85         }
86 
87         // PosTest5: Call GetCharCount with ASCII decoder and Arbitrary byte array
88         [Fact]
PosTest5()89         public void PosTest5()
90         {
91             Decoder decoder = Encoding.UTF8.GetDecoder();
92             byte[] bytes = new byte[c_SIZE_OF_ARRAY];
93             _generator.GetBytes(-55, bytes);
94 
95             decoder.GetCharCount(bytes, 0, bytes.Length, true);
96             decoder.GetCharCount(bytes, 0, bytes.Length, false);
97         }
98 
99         // PosTest6: Call GetCharCount with ASCII decoder and convert partial of ASCII array
100         [Fact]
PosTest6()101         public void PosTest6()
102         {
103             Decoder decoder = Encoding.UTF8.GetDecoder();
104             byte[] bytes = new byte[c_SIZE_OF_ARRAY];
105             int expected = bytes.Length / 2;
106             for (int i = 0; i < bytes.Length; ++i)
107             {
108                 bytes[i] = (byte)i;
109             }
110 
111             VerificationHelper(decoder, bytes, 0, expected, true, expected, "006.1");
112             VerificationHelper(decoder, bytes, 0, expected, false, expected, "006.2");
113             VerificationHelper(decoder, bytes, expected, expected, true, expected, "006.3");
114             VerificationHelper(decoder, bytes, 1, expected, false, expected, "006.4");
115         }
116 
117         // PosTest7: Call GetCharCount with Unicode decoder and convert partial of Unicode byte array
118         [Fact]
PosTest7()119         public void PosTest7()
120         {
121             Decoder decoder = Encoding.Unicode.GetDecoder();
122             // Unicode string: 这个一个测试
123             byte[] bytes = new byte[] {
124                 217,
125                 143,
126                 42,
127                 78,
128                 0,
129                 78,
130                 42,
131                 78,
132                 75,
133                 109,
134                 213,
135                 139
136             };
137             int expected = 3;
138 
139             VerificationHelper(decoder, bytes, 0, bytes.Length / 2, true, expected, "007.1");
140             VerificationHelper(decoder, bytes, 0, bytes.Length / 2, false, expected, "007.2");
141             VerificationHelper(decoder, bytes, bytes.Length / 2, 0, true, 0, "007.3");
142             // Set index to 1, so some characters may be not converted
143             VerificationHelper(decoder, bytes, 1, bytes.Length / 2, false, expected, "007.4");
144         }
145 
146         // PosTest8: Call GetCharCount with ASCII decoder and count = 0
147         [Fact]
PosTest8()148         public void PosTest8()
149         {
150             Decoder decoder = Encoding.UTF8.GetDecoder();
151             byte[] bytes = new byte[c_SIZE_OF_ARRAY];
152             int expected = 0;
153             for (int i = 0; i < bytes.Length; ++i)
154             {
155                 bytes[i] = (byte)i;
156             }
157 
158             VerificationHelper(decoder, bytes, 0, expected, true, expected, "008.1");
159             VerificationHelper(decoder, bytes, 0, expected, false, expected, "008.2");
160             VerificationHelper(decoder, bytes, 1, expected, true, expected, "008.3");
161             VerificationHelper(decoder, bytes, bytes.Length, expected, false, expected, "008.4");
162         }
163         #endregion
164 
VerificationHelper(Decoder decoder, byte[] bytes, int index, int count, bool flush, int expected, string errorno)165         private void VerificationHelper(Decoder decoder, byte[] bytes, int index, int count, bool flush, int expected, string errorno)
166         {
167             int ret = decoder.GetCharCount(bytes, index, count, flush);
168             Assert.Equal(expected, ret);
169         }
170     }
171 }
172