Merge pull request #5721 from eugenp/fix-matcher-test
fix regex matcher unit test
This commit is contained in:
		
						commit
						702495be5f
					
				| @ -1,6 +1,5 @@ | |||||||
| package com.baeldung.regexp.optmization; | package com.baeldung.regexp.optmization; | ||||||
| 
 | 
 | ||||||
| import org.junit.After; |  | ||||||
| import org.junit.Before; | import org.junit.Before; | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| 
 | 
 | ||||||
| @ -14,14 +13,16 @@ import static org.junit.Assert.assertTrue; | |||||||
| 
 | 
 | ||||||
| public class OptimizedMatcherUnitTest { | public class OptimizedMatcherUnitTest { | ||||||
| 
 | 
 | ||||||
|     private long time; |  | ||||||
|     private long mstimePreCompiled; |  | ||||||
|     private long mstimeNotPreCompiled; |  | ||||||
| 
 |  | ||||||
|     private String action; |     private String action; | ||||||
| 
 | 
 | ||||||
|     private List<String> items; |     private List<String> items; | ||||||
|      |      | ||||||
|  |     private class TimeWrapper { | ||||||
|  |         private long time; | ||||||
|  |         private long mstimePreCompiled; | ||||||
|  |         private long mstimeNotPreCompiled; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     @Before |     @Before | ||||||
|     public void setup() { |     public void setup() { | ||||||
|         Random random = new Random(); |         Random random = new Random(); | ||||||
| @ -48,27 +49,27 @@ public class OptimizedMatcherUnitTest { | |||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void givenANotPreCompiledAndAPreCompiledPatternA_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() { |     public void givenANotPreCompiledAndAPreCompiledPatternA_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() { | ||||||
| 
 |         TimeWrapper timeObj = new TimeWrapper(); | ||||||
|         testPatterns("A*"); |         testPatterns("A*", timeObj); | ||||||
|         assertTrue(mstimePreCompiled < mstimeNotPreCompiled); |         assertTrue(timeObj.mstimePreCompiled < timeObj.mstimeNotPreCompiled); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void givenANotPreCompiledAndAPreCompiledPatternABC_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() { |     public void givenANotPreCompiledAndAPreCompiledPatternABC_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() { | ||||||
| 
 |         TimeWrapper timeObj = new TimeWrapper(); | ||||||
|         testPatterns("A*B*C*"); |         testPatterns("A*B*C*", timeObj); | ||||||
|         assertTrue(mstimePreCompiled < mstimeNotPreCompiled); |         assertTrue(timeObj.mstimePreCompiled < timeObj.mstimeNotPreCompiled); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void givenANotPreCompiledAndAPreCompiledPatternECWF_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() { |     public void givenANotPreCompiledAndAPreCompiledPatternECWF_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled() { | ||||||
| 
 |         TimeWrapper timeObj = new TimeWrapper(); | ||||||
|         testPatterns("E*C*W*F*"); |         testPatterns("E*C*W*F*", timeObj); | ||||||
|         assertTrue(mstimePreCompiled < mstimeNotPreCompiled); |         assertTrue(timeObj.mstimePreCompiled < timeObj.mstimeNotPreCompiled); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private void testPatterns(String regex) { |     private void testPatterns(String regex, TimeWrapper timeObj) { | ||||||
|         time = System.nanoTime(); |         timeObj.time = System.nanoTime(); | ||||||
|         int matched = 0; |         int matched = 0; | ||||||
|         int unmatched = 0; |         int unmatched = 0; | ||||||
| 
 | 
 | ||||||
| @ -83,10 +84,10 @@ public class OptimizedMatcherUnitTest { | |||||||
| 
 | 
 | ||||||
|         this.action = "uncompiled: regex=" + regex + " matched=" + matched + " unmatched=" + unmatched; |         this.action = "uncompiled: regex=" + regex + " matched=" + matched + " unmatched=" + unmatched; | ||||||
| 
 | 
 | ||||||
|         this.mstimeNotPreCompiled = (System.nanoTime() - time) / 1000000; |         timeObj.mstimeNotPreCompiled = (System.nanoTime() - timeObj.time) / 1000000; | ||||||
|         System.out.println(this.action + ": " + mstimeNotPreCompiled + "ms"); |         System.out.println(this.action + ": " + timeObj.mstimeNotPreCompiled + "ms"); | ||||||
| 
 | 
 | ||||||
|         time = System.nanoTime(); |         timeObj.time = System.nanoTime(); | ||||||
| 
 | 
 | ||||||
|         Matcher matcher = Pattern.compile(regex).matcher(""); |         Matcher matcher = Pattern.compile(regex).matcher(""); | ||||||
|         matched = 0; |         matched = 0; | ||||||
| @ -103,7 +104,7 @@ public class OptimizedMatcherUnitTest { | |||||||
| 
 | 
 | ||||||
|         this.action = "compiled: regex=" + regex + " matched=" + matched + " unmatched=" + unmatched; |         this.action = "compiled: regex=" + regex + " matched=" + matched + " unmatched=" + unmatched; | ||||||
| 
 | 
 | ||||||
|         this.mstimePreCompiled = (System.nanoTime() - time) / 1000000; |         timeObj.mstimePreCompiled = (System.nanoTime() - timeObj.time) / 1000000; | ||||||
|         System.out.println(this.action + ": " + mstimePreCompiled + "ms"); |         System.out.println(this.action + ": " + timeObj.mstimePreCompiled + "ms"); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user