Skip to content

Commit 318ff8e

Browse files
committed
debug
1 parent 9fe96eb commit 318ff8e

2 files changed

Lines changed: 27 additions & 31 deletions

File tree

src/lib.nr

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ where
102102
// because `self.keys` is sorted, we can simply validate that
103103
// sorted_keys.sorted[0] < 2^32
104104
// sorted_keys.sorted[N-1] < maximum
105+
assert(_maximum >= sorted_keys.sorted[N - 1]);
105106
r
106107
}
107108

@@ -149,6 +150,12 @@ where
149150
// `self.keys[found_index] + 1 - found <= idx <= self.keys[found_index + 1 - found] - 1 + found
150151
let lhs = self.keys[found_index];
151152
let rhs = self.keys[found_index + 1 - found];
153+
//let lhs_condition = idx - lhs - 1 + found;
154+
assert(lhs + 1 - found <= idx);
155+
assert(idx <= rhs + found - 1);
156+
//let rhs_condition = rhs - 1 + found - idx;
157+
//lhs_condition.assert_max_bit_size::<32>();
158+
//rhs_condition.assert_max_bit_size::<32>();
152159

153160
// self.keys[i] maps to self.values[i+1]
154161
// however...if we did not find a non-sparse entry, we want to return self.values[0] (the default value)
@@ -180,27 +187,25 @@ mod test {
180187
fn test_sparse_lookup_boundary_cases() {
181188
// what about when keys[0] = 0 and keys[N-1] = 2^32 - 1?
182189
let example = SparseArray::create(
183-
[0, 99999, 7, 0xffffffff],
190+
[0, 99999, 7, 0xfffffffe],
184191
[123, 101112, 789, 456],
185-
0xFFFFFFFF,
192+
0xffffffff,
186193
);
187194

188195
assert(example.get(0) == 123);
189196
assert(example.get(99999) == 101112);
190197
assert(example.get(7) == 789);
191-
assert(example.get(0xffffffff) == 456);
192-
assert(example.get(0xfffffffe) == 0);
198+
assert(example.get(0xfffffffe) == 456);
199+
assert(example.get(0xfffffffd) == 0);
193200
}
194201

195-
/**
196-
#[test(should_fail_with = "call to assert_max_bit_size")]
202+
#[test(should_fail)]
197203
fn test_sparse_lookup_overflow() {
198204
let example = SparseArray::create([1, 5, 7, 99999], [123, 456, 789, 101112], 100000);
199205

200206
assert(example.get(100000) == 0);
201207
}
202-
**/
203-
208+
204209
/**
205210
#[test(should_fail_with = "call to assert_max_bit_size")]
206211
fn test_sparse_lookup_boundary_case_overflow() {
@@ -210,7 +215,7 @@ mod test {
210215
assert(example.get(0x100000000) == 0);
211216
}
212217
**/
213-
#[test(should_fail_with = "call to assert_max_bit_size")]
218+
#[test(should_fail)]
214219
fn test_sparse_lookup_key_exceeds_maximum() {
215220
let example =
216221
SparseArray::create([0, 5, 7, 0xffffffff], [123, 456, 789, 101112], 0xffffffff);

src/mut_sparse_array.nr

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ where
8888
r.linked_keys[i] = i + 1;
8989
}
9090
// set the last linked key to 2^32 - 1
91-
r.linked_keys[M + 1] = 0xFFFFFFFF;
91+
r.linked_keys[M + 1] = 0xFFFFFFFF;
9292

9393
// populate values based on the sorted keys
9494
// note: self.keys[i] maps to self.values[i+1]
@@ -117,12 +117,9 @@ where
117117
// because `self.keys` is sorted, we can simply validate that
118118
// sorted_keys.sorted[0] < 2^32
119119
// sorted_keys.sorted[N-1] < maximum
120-
//ComparisonFuncs::assert_greater_than_or_equal(sorted_keys.sorted[0], 0);
121-
//ComparisonFuncs::assert_greater_than_or_equal(_maximum, 0);
122-
//ComparisonFuncs::assert_greater_than_or_equal(_maximum, sorted_keys.sorted[M - 1]);
123120

124-
// _maximum.assert_max_bit_size::<32>();
125-
// (_maximum - sorted_keys.sorted[M - 1]).assert_max_bit_size::<32>();
121+
let val = sorted_keys.sorted[M - 1];
122+
assert(_maximum >= sorted_keys.sorted[M - 1]);
126123
r.tail_ptr = M + 2;
127124
r
128125
}
@@ -183,13 +180,10 @@ where
183180
// `self.keys[found_index] + 1 - found <= idx <= self.keys[found_index + 1 - found] - 1 + found
184181
let lhs = self.keys[lhs_index];
185182
let rhs = self.keys[rhs_index];
186-
let lhs_condition = idx + found - lhs - 1;
187-
let rhs_condition = rhs + found - 1 - idx;
188-
//ComparisonFuncs::assert_greater_than_or_equal(lhs_condition, 0);
189-
//ComparisonFuncs::assert_greater_than_or_equal(rhs_condition, 0);
190183

191-
// lhs_condition.assert_max_bit_size::<32>();
192-
// rhs_condition.assert_max_bit_size::<32>();
184+
assert(lhs + 1 - found <= idx);
185+
assert(idx <= rhs + found - 1);
186+
193187
// lhs points to tail_ptr
194188
// tail_ptr points to rhs
195189
if (found == 0) {
@@ -207,7 +201,6 @@ where
207201

208202
fn get(self, idx: u32) -> T {
209203
let (found, found_index) = unsafe { self.search_for_key(idx) };
210-
println(f"f {found} fi {found_index}");
211204
assert(found * found == found);
212205

213206
let lhs_index = found_index;
@@ -225,7 +218,7 @@ where
225218
// `self.keys[found_index] + 1 - found <= idx <= self.keys[found_index + 1 - found] - 1 + found
226219
let lhs = self.keys[lhs_index];
227220
let rhs = self.keys[rhs_index];
228-
let lhs_condition = idx + found- lhs - 1;
221+
let lhs_condition = idx + found - lhs - 1;
229222
let rhs_condition = rhs + found - 1 - idx;
230223
//ComparisonFuncs::assert_greater_than_or_equal(lhs_condition, 0);
231224
//ComparisonFuncs::assert_greater_than_or_equal(rhs_condition, 0);
@@ -284,16 +277,16 @@ mod test {
284277
fn test_sparse_lookup_boundary_cases() {
285278
// what about when keys[0] = 0 and keys[N-1] = 2^32 - 1?
286279
let example: MutSparseArray<6, _> = MutSparseArray::create(
287-
[0, 99999, 7, 0xffffffff],
280+
[0, 99999, 7, 0xfffffffe],
288281
[123, 101112, 789, 456],
289282
0xFFFFFFFF,
290283
);
291284

292285
assert(example.get(0) == 123);
293286
assert(example.get(99999) == 101112);
294287
assert(example.get(7) == 789);
295-
assert(example.get(0xffffffff) == 456);
296-
assert(example.get(0xfffffffe) == 0);
288+
assert(example.get(0xfffffffe) == 456);
289+
assert(example.get(0xfffffffd) == 0);
297290
}
298291

299292
#[test]
@@ -313,25 +306,23 @@ mod test {
313306
assert(example.get(100000) == 0);
314307
}
315308

316-
#[test(should_fail_with = "call to assert_max_bit_size")]
309+
#[test(should_fail)]
317310
fn test_sparse_lookup_overflow() {
318311
let example: MutSparseArray<8, _> =
319312
MutSparseArray::create([1, 5, 7, 99999], [123, 456, 789, 101112], 100000);
320313

321314
assert(example.get(100000) == 0);
322315
}
323316

324-
/**
325-
#[test(should_fail_with = "call to assert_max_bit_size")]
317+
#[test(should_fail)]
326318
fn test_sparse_lookup_boundary_case_overflow() {
327319
let example: MutSparseArray<4, _> =
328320
MutSparseArray::create([0, 5, 7, 0xffffffff], [123, 456, 789, 101112], 0xFFFFFFFF);
329321

330322
assert(example.get(0xFFFFFFFF) == 0);
331323
}
332-
**/
333324

334-
#[test(should_fail_with = "call to assert_max_bit_size")]
325+
#[test(should_fail)]
335326
fn test_sparse_lookup_key_exceeds_maximum() {
336327
let example: MutSparseArray<6, _> =
337328
MutSparseArray::create([0, 5, 7, 0xffffffff], [123, 456, 789, 101112], 0xffffffff);

0 commit comments

Comments
 (0)