Project

General

Profile

Tmp » History » Revision 4

Revision 3 (Николай Пакулин, 03/30/2015 05:56 PM) → Revision 4/6 (Николай Пакулин, 03/30/2015 06:10 PM)

h1. Кусочки кода 

 <pre> 
 static void assertTestString(CharSequence s) { 
		 int pos = 0; 
		 int len = s.length(); 
		 int rest = 0; 
		 char fill = 0; 
		
		 for (pos = 0; pos < len; pos++) { 
			 if (rest == 0) { 
				 fill = s.charAt(pos); 
				 if (fill == 0) { 
					 continue; 
				 } 
				 rest = fill - 1; 
				 continue; 
			 } else { 
				 assertEquals("Position " + pos + ": expected symbol \\u"  
						 + new Integer(fill) + " got \\u"  
						 + new Integer(s.charAt(pos)) 
					 , fill, s.charAt(pos)); 
				 rest --; 
			 } 
		 } 
		 assertEquals("expected "+ rest + " more characters",  
				 0, rest); 
	 } 
 </pre> 
 <pre> 
         @Test 
	 public void testAssert() { 
		 assertTestString("\u0000"); 
		 assertTestString("\u0001"); 
		 assertTestString("\u0002\u0002\u0003\u0003\u0003"); 
	 } 
	
	 @Test 
	 public void testAssertFail() { 
		 String[] tests = new String[] { 
				 "\u0002", 
				 "\u0002\u0002\u0003" 
		 }; 
		 for (int i = 0; i < tests.length; i++) { 
			 try{ 
				 assertTestString(tests[i]); 
			 }catch (AssertionError e) { 
				 continue; 
			 } 
			 fail("Missed wrong string {" + tests[i] + "}"); 
		 } 
	 } 


 </pre> 

 <pre> 
	 public static class Worker implements Runnable{ 
		 MyStringBuilder target; 
		 BlockingQueue<String> q; 
		 public Worker(MyStringBuilder target, BlockingQueue<String> q) { 
			 super(); 
			 this.target = target; 
			 this.q = q; 
		 } 
		 @Override 
		 public void run() { 
			 String s = null; 
			 while(true) { 
				 try { 
					 s = q.take(); 
					 System.out.println("Thread #" + Thread.currentThread().getId() + ":Taken " + s.length()); 
					 if (s.length() == 0) return; 
				 } catch (InterruptedException e) { 
					 // TODO Auto-generated catch block 
					 e.printStackTrace(); 
					 continue; 
				 } 
				
				 target.append(s); 
			 } 
		 } 	
	 } 
 </pre>