@@ -28,6 +28,104 @@ task :release do
2828 sh "git release"
2929end
3030
31+ require "securerandom"
32+ require "./lib/cipherstash/index"
33+ require "json"
34+
35+ task :generate_ordered_string_test_cases do
36+ id = SecureRandom . uuid
37+ settings = {
38+ "meta" => {
39+ "$indexId" => id ,
40+ "$indexName" => "titleSort" ,
41+ "$prfKey" => SecureRandom . hex ( 16 ) ,
42+ "$prpKey" => SecureRandom . hex ( 16 ) ,
43+ } ,
44+ "mapping" => {
45+ "kind" => "range" ,
46+ "field" => "title" ,
47+ "fieldType" => "string" ,
48+ }
49+ }
50+ schema_versions = { :first => 0 , :last => 0 , :searchable => true }
51+
52+ index = CipherStash ::Index . generate ( id , settings , schema_versions )
53+
54+ random_ascii_string = -> ( ) do
55+ unicode_char_max = 127
56+ max_string_length = 200
57+ ( 0 ..rand ( max_string_length -1 ) ) . map { rand ( unicode_char_max + 1 ) . chr } . join
58+ end
59+
60+ num_test_cases = 1_000
61+
62+ orderise_string_cases = ( 0 ..( num_test_cases - 1 ) ) . map do
63+ str = random_ascii_string . call
64+ output = index . __send__ :orderise_string , str
65+ { input : str , output : output }
66+ end
67+
68+ File . write ( "orderise_string_test_cases.json" , orderise_string_cases . to_json )
69+
70+ num_test_cases = 100
71+
72+ string_comparison_cases = ( 0 ..( num_test_cases - 1 ) ) . map do
73+ # TODO: this only gives single-char strings of alpha chars for debugging
74+ # why this test case doesn't work. This looks like a bug on the ruby side.
75+ # The comparison logic using encrypted terms is busted.
76+ # random_ascii_string = -> () do
77+ # max_string_length = 10
78+ # (0..0).map { (rand(26) + 97).chr }.join
79+ # end
80+
81+ # str_a = random_ascii_string.call
82+ # terms_a = index.analyze(SecureRandom.uuid, {"title" => str_a})[:terms]
83+
84+ # if terms_a.length != 1
85+ # raise "Expected terms_a to have exactly one item. Had #{terms_a.length}"
86+ # end
87+
88+ # term_a = terms_a.first[:term]
89+
90+ # str_b = random_ascii_string.call
91+ # terms_b = index.analyze(SecureRandom.uuid, {"title" => str_b})[:terms]
92+
93+ # if terms_b.length != 1
94+ # raise "Expected terms_b to have exactly one item. Had #{terms_b.length}"
95+ # end
96+
97+ # term_b = terms_b.first[:term]
98+
99+ # output = case term_a <=> term_b
100+ # when -1
101+ # "<"
102+ # when 0
103+ # "=="
104+ # when 1
105+ # ">"
106+ # end
107+
108+ str_a = random_ascii_string . call
109+ terms_a = index . __send__ :orderise_string , str_a
110+
111+ str_b = random_ascii_string . call
112+ terms_b = index . __send__ :orderise_string , str_b
113+
114+ output = case terms_a <=> terms_b
115+ when -1
116+ "<"
117+ when 0
118+ "=="
119+ when 1
120+ ">"
121+ end
122+
123+ { input : [ str_a , str_b ] , output : output }
124+ end
125+
126+ File . write ( "string_comparison_test_cases.json" , string_comparison_cases . to_json )
127+ end
128+
31129require 'yard'
32130
33131YARD ::Rake ::YardocTask . new :doc do |yardoc |
0 commit comments