mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	* test/ruby/allpairs.rb: new file for all pairs method.
* test/ruby/test_m17n_comb.rb: use allpairs.rb to reduce test cases. * test/ruby/test_sprintf_comb.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15658 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
		
							parent
							
								
									fe7d645eed
								
							
						
					
					
						commit
						00bf4e8a84
					
				
					 4 changed files with 119 additions and 28 deletions
				
			
		|  | @ -1,3 +1,11 @@ | |||
| Sat Mar  1 13:11:03 2008  Tanaka Akira  <akr@fsij.org> | ||||
| 
 | ||||
| 	* test/ruby/allpairs.rb: new file for all pairs method. | ||||
| 
 | ||||
| 	* test/ruby/test_m17n_comb.rb: use allpairs.rb to reduce test cases. | ||||
| 
 | ||||
| 	* test/ruby/test_sprintf_comb.rb: ditto. | ||||
| 
 | ||||
| Sat Mar  1 12:34:21 2008  Yukihiro Matsumoto  <matz@ruby-lang.org> | ||||
| 
 | ||||
| 	* string.c (sym_inspect): use rb_str_inspect() instead of | ||||
|  |  | |||
							
								
								
									
										103
									
								
								test/ruby/allpairs.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										103
									
								
								test/ruby/allpairs.rb
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,103 @@ | |||
| module AllPairs | ||||
|   module_function | ||||
| 
 | ||||
|   def make_prime(v) | ||||
|     return 2 if v < 2 | ||||
|     ary = [true] * (v*2) | ||||
|     2.upto(Math.sqrt(ary.length).ceil) {|i| | ||||
|       return i if ary[i] && v <= i | ||||
|       (i*2).step(ary.length, i) {|j| | ||||
|         ary[j] = false | ||||
|       } | ||||
|     } | ||||
|     v.upto(ary.length-1) {|i| | ||||
|       return i if ary[i] | ||||
|     } | ||||
|     raise "[bug] prime not found greater than #{v}" | ||||
|   end | ||||
| 
 | ||||
|   def make_basic_block(prime) | ||||
|     prime.times {|i| | ||||
|       prime.times {|j| | ||||
|         row = [i] | ||||
|         0.upto(prime-1) {|m| | ||||
|           row << (i*m + j) % prime | ||||
|         } | ||||
|         yield row | ||||
|       } | ||||
|     } | ||||
|   end  | ||||
| 
 | ||||
|   def combine_block(tbl1, tbl2) | ||||
|     result = [] | ||||
|     tbl2.each {|row| | ||||
|       result << row * tbl1.first.length | ||||
|     } | ||||
|     tbl1.each_with_index {|row, k| | ||||
|       next if k == 0 | ||||
|       result << row.map {|i| [i] * tbl2.first.length }.flatten | ||||
|     } | ||||
|     result | ||||
|   end | ||||
| 
 | ||||
|   def make_large_block(v, prime) | ||||
|     if prime <= v+1 | ||||
|       make_basic_block(v) {|row| | ||||
|         yield row | ||||
|       } | ||||
|     else | ||||
|       tbl = [] | ||||
|       make_basic_block(v) {|row| | ||||
|         tbl << row | ||||
|       }  | ||||
|       tbls = [tbl] | ||||
|       while tbl.first.length ** 2 < prime | ||||
|         tbl = combine_block(tbl, tbl) | ||||
|         tbls << tbl | ||||
|       end | ||||
|       tbl1 = tbls.find {|t| prime <= t.first.length * tbl.first.length } | ||||
|       tbl = combine_block(tbl, tbl1) | ||||
|       tbl.each {|row| | ||||
|         yield row | ||||
|       } | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   def each_index(*vs) | ||||
|     n = vs.length | ||||
|     max_v = vs.max | ||||
|     prime = make_prime(max_v) | ||||
|     h = {} | ||||
|     make_large_block(max_v, n) {|row| | ||||
|       row = vs.zip(row).map {|v, i| i % v } | ||||
|       next if h[row] | ||||
|       h[row] = true | ||||
|       yield row | ||||
|     } | ||||
|   end | ||||
| 
 | ||||
|   # generate all pairs test. | ||||
|   def each(*args) | ||||
|     args.map! {|a| a.to_a } | ||||
|     each_index(*args.map {|a| a.length}) {|is| | ||||
|       yield is.zip(args).map {|i, a| a[i] } | ||||
|     } | ||||
|   end | ||||
| 
 | ||||
|   # generate all combination in cartesian product.  (not all-pairs test) | ||||
|   def exhaustive_each(*args) | ||||
|     args = args.map {|a| a.to_a } | ||||
|     i = 0 | ||||
|     while true | ||||
|       n = i | ||||
|       as = [] | ||||
|       args.reverse_each {|a| | ||||
|         n, m = n.divmod(a.length) | ||||
|         as.unshift a[m] | ||||
|       } | ||||
|       break if 0 < n | ||||
|       yield as | ||||
|       i += 1 | ||||
|     end | ||||
|   end | ||||
| end | ||||
|  | @ -1,5 +1,7 @@ | |||
| require 'test/unit' | ||||
| require 'stringio' | ||||
| require 'require_relative' | ||||
| require_relative 'allpairs' | ||||
| 
 | ||||
| class TestM17NComb < Test::Unit::TestCase | ||||
|   def assert_encoding(encname, actual, message=nil) | ||||
|  | @ -113,20 +115,8 @@ class TestM17NComb < Test::Unit::TestCase | |||
|     #"aaa".force_encoding("utf-32be"), | ||||
|   ] | ||||
| 
 | ||||
|   def combination(*args) | ||||
|     args = args.map {|a| a.to_a } | ||||
|     i = 0 | ||||
|     while true | ||||
|       n = i | ||||
|       as = [] | ||||
|       args.reverse_each {|a| | ||||
|         n, m = n.divmod(a.length) | ||||
|         as.unshift a[m] | ||||
|       } | ||||
|       break if 0 < n | ||||
|       yield as | ||||
|       i += 1 | ||||
|     end | ||||
|   def combination(*args, &b) | ||||
|     AllPairs.each(*args, &b) | ||||
|   end | ||||
| 
 | ||||
|   def encdump(str) | ||||
|  |  | |||
|  | @ -1,4 +1,6 @@ | |||
| require 'test/unit' | ||||
| require 'require_relative' | ||||
| require_relative 'allpairs' | ||||
| 
 | ||||
| class TestSprintfComb < Test::Unit::TestCase | ||||
|   VS = [ | ||||
|  | @ -106,20 +108,8 @@ class TestSprintfComb < Test::Unit::TestCase | |||
|   ] | ||||
|   VS.reverse! | ||||
| 
 | ||||
|   def combination(*args) | ||||
|     args = args.map {|a| a.to_a } | ||||
|     i = 0 | ||||
|     while true | ||||
|       n = i | ||||
|       as = [] | ||||
|       args.reverse_each {|a| | ||||
|         n, m = n.divmod(a.length) | ||||
|         as.unshift a[m] | ||||
|       } | ||||
|       break if 0 < n | ||||
|       yield as | ||||
|       i += 1 | ||||
|     end | ||||
|   def combination(*args, &b) | ||||
|     AllPairs.each(*args, &b) | ||||
|   end | ||||
| 
 | ||||
|   def emu(format, v) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 akr
						akr