Tmp » History » Version 2
Николай Пакулин, 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 | 2 | Николай Пакулин | @Test |
31 | public void testAssert() { |
||
32 | assertTestString("\u0000"); |
||
33 | assertTestString("\u0001"); |
||
34 | assertTestString("\u0002\u0002\u0003\u0003\u0003"); |
||
35 | } |
||
36 | |||
37 | @Test |
||
38 | public void testAssertFail() { |
||
39 | String[] tests = new String[] { |
||
40 | "\u0002", |
||
41 | "\u0002\u0002\u0003" |
||
42 | }; |
||
43 | for (int i = 0; i < tests.length; i++) { |
||
44 | try{ |
||
45 | assertTestString(tests[i]); |
||
46 | }catch (AssertionError e) { |
||
47 | continue; |
||
48 | } |
||
49 | fail("Missed wrong string {" + tests[i] + "}"); |
||
50 | } |
||
51 | } |
||
52 | <pre> |
||
53 | |||
54 | </pre> |