Tmp » History » Version 3
Николай Пакулин, 03/30/2015 05:56 PM
1 | 1 | Николай Пакулин | h1. Кусочки кода |
---|---|---|---|
2 | |||
3 | <pre> |
||
4 | static void assertTestString(CharSequence s) { |
||
5 | int pos = 0; |
||
6 | int len = s.length(); |
||
7 | int rest = 0; |
||
8 | char fill = 0; |
||
9 | |||
10 | for (pos = 0; pos < len; pos++) { |
||
11 | if (rest == 0) { |
||
12 | fill = s.charAt(pos); |
||
13 | if (fill == 0) { |
||
14 | continue; |
||
15 | } |
||
16 | rest = fill - 1; |
||
17 | continue; |
||
18 | } else { |
||
19 | assertEquals("Position " + pos + ": expected symbol \\u" |
||
20 | + new Integer(fill) + " got \\u" |
||
21 | + new Integer(s.charAt(pos)) |
||
22 | , fill, s.charAt(pos)); |
||
23 | rest --; |
||
24 | } |
||
25 | } |
||
26 | assertEquals("expected "+ rest + " more characters", |
||
27 | 0, rest); |
||
28 | } |
||
29 | </pre> |
||
30 | 3 | Николай Пакулин | <pre> |
31 | 2 | Николай Пакулин | @Test |
32 | public void testAssert() { |
||
33 | assertTestString("\u0000"); |
||
34 | assertTestString("\u0001"); |
||
35 | assertTestString("\u0002\u0002\u0003\u0003\u0003"); |
||
36 | } |
||
37 | |||
38 | @Test |
||
39 | public void testAssertFail() { |
||
40 | String[] tests = new String[] { |
||
41 | "\u0002", |
||
42 | "\u0002\u0002\u0003" |
||
43 | }; |
||
44 | for (int i = 0; i < tests.length; i++) { |
||
45 | try{ |
||
46 | assertTestString(tests[i]); |
||
47 | }catch (AssertionError e) { |
||
48 | continue; |
||
49 | } |
||
50 | fail("Missed wrong string {" + tests[i] + "}"); |
||
51 | } |
||
52 | 1 | Николай Пакулин | } |
53 | 3 | Николай Пакулин | |
54 | 2 | Николай Пакулин | |
55 | </pre> |