@@ -2,14 +2,22 @@ use crate::{
22 note ::{
33 ConfirmedNote ,
44 HintedNote ,
5- note_getter ::{confirm_hinted_note , confirm_hinted_notes , extract_property_value_from_selector },
6- note_getter_options ::{NoteGetterOptions , PropertySelector , SortOrder },
5+ note_getter ::{
6+ assert_notes_order , confirm_hinted_note , confirm_hinted_notes , extract_property_value_from_selector ,
7+ },
8+ note_getter_options ::{NoteGetterOptions , PropertySelector , Sort , SortOrder },
79 },
810 oracle::random:: random ,
911 test ::{helpers::test_environment::TestEnvironment , mocks::mock_note::MockNote },
1012 utils::comparison::Comparator ,
1113};
1214
15+ fn sort_criterion (index : u8 , order : u8 ) -> Option <Sort > {
16+ Option ::some (
17+ Sort ::new (PropertySelector { index , offset : 0 , length : 32 }, order ),
18+ )
19+ }
20+
1321use crate::protocol:: {address::AztecAddress , traits::FromField };
1422
1523global storage_slot : Field = 42 ;
@@ -388,3 +396,63 @@ unconstrained fn extract_property_value_fails_on_oob_index() {
388396 let selector = PropertySelector { index : 1 , offset : 0 , length : 1 };
389397 let _ = extract_property_value_from_selector (packed , selector );
390398}
399+
400+ global ASC_DESC_CRITERIA : BoundedVec <Option <Sort >, 2 > = BoundedVec ::from_array ([
401+ sort_criterion (0 , SortOrder .ASC ),
402+ sort_criterion (1 , SortOrder .DESC ),
403+ ]);
404+
405+ #[test]
406+ unconstrained fn assert_notes_order_passes_when_first_criterion_satisfied () {
407+ assert_notes_order ([1 , 10 ], [2 , 999 ], ASC_DESC_CRITERIA );
408+ }
409+
410+ #[test]
411+ unconstrained fn assert_notes_order_passes_when_second_criterion_satisfied_after_tie () {
412+ assert_notes_order ([1 , 10 ], [1 , 5 ], ASC_DESC_CRITERIA );
413+ }
414+
415+ #[test(should_fail_with = "Return notes not sorted in descending order.")]
416+ unconstrained fn assert_notes_order_fails_when_second_criterion_violated_after_tie () {
417+ assert_notes_order ([1 , 5 ], [1 , 10 ], ASC_DESC_CRITERIA );
418+ }
419+
420+ #[test]
421+ unconstrained fn assert_notes_order_passes_when_all_fields_equal () {
422+ assert_notes_order ([1 , 10 ], [1 , 10 ], ASC_DESC_CRITERIA );
423+ }
424+
425+ global DESC_ONLY_CRITERIA : BoundedVec <Option <Sort >, 2 > = BoundedVec ::from_array ([sort_criterion (0 , SortOrder .DESC )]);
426+
427+ #[test]
428+ unconstrained fn assert_notes_order_passes_with_single_criterion () {
429+ assert_notes_order ([10 , 1 ], [5 , 20 ], DESC_ONLY_CRITERIA );
430+ }
431+
432+ #[test]
433+ unconstrained fn assert_notes_order_passes_with_single_criterion_when_field_ties () {
434+ assert_notes_order ([5 , 1 ], [5 , 20 ], DESC_ONLY_CRITERIA );
435+ }
436+
437+ global REVERSED_CRITERIA : BoundedVec <Option <Sort >, 2 > = BoundedVec ::from_array ([
438+ sort_criterion (1 , SortOrder .ASC ),
439+ sort_criterion (0 , SortOrder .DESC ),
440+ ]);
441+
442+ #[test]
443+ unconstrained fn assert_notes_order_uses_criterion_order_not_field_order () {
444+ // field_0 is 10 > 5 (would fail ASC), but the first criterion targets field_1
445+ assert_notes_order ([10 , 1 ], [5 , 2 ], REVERSED_CRITERIA );
446+ }
447+
448+ #[test(should_fail_with = "Return notes not sorted in ascending order.")]
449+ unconstrained fn assert_notes_order_fails_when_criterion_order_not_field_order () {
450+ // First criterion is field_1 ASC: 5 > 2 violates ASC
451+ assert_notes_order ([1 , 5 ], [2 , 2 ], REVERSED_CRITERIA );
452+ }
453+
454+ #[test]
455+ unconstrained fn assert_notes_order_passes_with_no_criteria () {
456+ let criteria : BoundedVec <Option <Sort >, 2 > = BoundedVec ::new ();
457+ assert_notes_order ([2 , 5 ], [1 , 10 ], criteria );
458+ }
0 commit comments