Project

General

Profile

Tmp » History » Version 4

Николай Пакулин, 03/30/2015 06:10 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>
56 4 Николай Пакулин
57
<pre>
58
	public static class Worker implements Runnable{
59
		MyStringBuilder target;
60
		BlockingQueue<String> q;
61
		public Worker(MyStringBuilder target, BlockingQueue<String> q) {
62
			super();
63
			this.target = target;
64
			this.q = q;
65
		}
66
		@Override
67
		public void run() {
68
			String s = null;
69
			while(true) {
70
				try {
71
					s = q.take();
72
					System.out.println("Thread #" + Thread.currentThread().getId() + ":Taken " + s.length());
73
					if (s.length() == 0) return;
74
				} catch (InterruptedException e) {
75
					// TODO Auto-generated catch block
76
					e.printStackTrace();
77
					continue;
78
				}
79
				
80
				target.append(s);
81
			}
82
		}	
83
	}
84
</pre>