Skip to content

Commit

Permalink
OrderedMap/Set: remove Debug modules & add validate()
Browse files Browse the repository at this point in the history
  • Loading branch information
GoPavel committed Nov 13, 2024
1 parent b5f7143 commit 2119459
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 61 deletions.
45 changes: 24 additions & 21 deletions src/OrderedMap.mo
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,12 @@ module {
/// where `n` denotes the number of key-value entries stored in the map.
public func some<V>(m : Map<K, V>, pred : (K, V) -> Bool) : Bool
= Internal.some(m.root, pred);

/// Debug helper that check internal invariants of the given map `m`.
/// Raise an error (for a stack trace) if invariants are violated.
public func validate<V>(m : Map<K, V>) : () {
Internal.validate(m, compare);
};
};

module Internal {
Expand Down Expand Up @@ -1152,27 +1158,10 @@ module {
};
case other { (other, y0) }
}
}
};

/// Create `OrderedMap.Operations` object capturing key type `K` and `compare` function.
/// It is an alias for the `Operations` constructor.
///
/// Example:
/// ```motoko
/// import Map "mo:base/OrderedMap";
/// import Nat "mo:base/Nat";
///
/// actor {
/// let natMap = Map.Make<Nat>(Nat.compare);
/// stable var map : Map.Map<Nat, Text> = natMap.empty<Text>();
/// };
/// ```
public let Make : <K>(compare : (K, K) -> O.Order) -> Operations<K> = Operations;
};

/// Test helpers
public module MapDebug {
public func checkMapInvariants<K, V>(rbMap : Map<K, V>, comp : (K, K) -> O.Order) {
// Test helper
public func validate<K, V>(rbMap : Map<K, V>, comp : (K, K) -> O.Order) {
ignore blackDepth(rbMap.root, comp)
};

Expand Down Expand Up @@ -1217,6 +1206,20 @@ module {
}
}
};
};

}
/// Create `OrderedMap.Operations` object capturing key type `K` and `compare` function.
/// It is an alias for the `Operations` constructor.
///
/// Example:
/// ```motoko
/// import Map "mo:base/OrderedMap";
/// import Nat "mo:base/Nat";
///
/// actor {
/// let natMap = Map.Make<Nat>(Nat.compare);
/// stable var map : Map.Map<Nat, Text> = natMap.empty<Text>();
/// };
/// ```
public let Make : <K>(compare : (K, K) -> O.Order) -> Operations<K> = Operations
}
47 changes: 23 additions & 24 deletions src/OrderedSet.mo
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,12 @@ module {
/// where `n` denotes the number of elements stored in the set.
public func some(s : Set<T>, pred : (T) -> Bool) : Bool
= Internal.some(s.root, pred);

/// Test helper that check internal invariant for the given set `s`.
/// Raise an error (for a stack trace) if invariants are violated.
public func validate(s : Set<T>): () {
Internal.validate(s, compare);
}
};

module Internal {
Expand Down Expand Up @@ -1154,32 +1160,10 @@ module {
};
{ root = newRoot;
size = if changed { s.size -1 } else { s.size } }
}
};

/// Create `OrderedSet.Operations` object capturing element type `T` and `compare` function.
/// It is an alias for the `Operations` constructor.
///
/// Example:
/// ```motoko
/// import Set "mo:base/OrderedSet";
/// import Nat "mo:base/Nat";
///
/// actor {
/// let natSet = Set.Make<Nat>(Nat.compare);
/// stable var set : Set.Set<Nat> = natSet.empty();
/// };
/// ```
public let Make : <T>(compare : (T, T) -> O.Order) -> Operations<T> = Operations;

/// Test helpers
public module SetDebug {
public func buildFromSorted<T>(a : [T]) : Set<T> {
{ root = Internal.buildFromSorted(Buffer.fromArray<T>(a)); size = a.size()}
};

// check binary search tree order of elements and black depth invariant of the RB-tree
public func checkSetInvariants<T>(s : Set<T>, comp : (T, T) -> O.Order) {
public func validate<T>(s : Set<T>, comp : (T, T) -> O.Order) {
ignore blackDepth(s.root, comp)
};

Expand Down Expand Up @@ -1223,5 +1207,20 @@ module {
}
}
}
}
};

/// Create `OrderedSet.Operations` object capturing element type `T` and `compare` function.
/// It is an alias for the `Operations` constructor.
///
/// Example:
/// ```motoko
/// import Set "mo:base/OrderedSet";
/// import Nat "mo:base/Nat";
///
/// actor {
/// let natSet = Set.Make<Nat>(Nat.compare);
/// stable var set : Set.Set<Nat> = natSet.empty();
/// };
/// ```
public let Make : <T>(compare : (T, T) -> O.Order) -> Operations<T> = Operations
}
2 changes: 1 addition & 1 deletion test/OrderedMap.prop.test.mo
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func run_all_props(range: (Nat, Nat), size: Nat, map_samples: Nat, query_samples
]),

prop("search tree invariant", func (m) {
Map.MapDebug.checkMapInvariants(m, Nat.compare);
natMap.validate(m);
true
}),

Expand Down
2 changes: 1 addition & 1 deletion test/OrderedMap.test.mo
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MapMatcher(expected : [(Nat, Text)]) : M.Matcher<Map.Map<Nat, Text>> {
}
};

func checkMap(m: Map.Map<Nat, Text>) { Map.MapDebug.checkMapInvariants(m, Nat.compare); };
func checkMap(m: Map.Map<Nat, Text>) { natMap.validate(m); };

func insert(rbTree : Map.Map<Nat, Text>, key : Nat) : Map.Map<Nat, Text> {
let updatedTree = natMap.put(rbTree, key, debug_show (key));
Expand Down
13 changes: 1 addition & 12 deletions test/OrderedSet.prop.test.mo
Original file line number Diff line number Diff line change
Expand Up @@ -225,19 +225,8 @@ func run_all_props(range: (Nat, Nat), size: Nat, set_samples: Nat, query_samples

suite(("Internal"), [
prop("search tree invariant", func (s) {
Set.SetDebug.checkSetInvariants<Nat>(s, Nat.compare);
natSet.validate(s);
true
}),
prop("buildFromSorted makes RB tree", func (s) {
let a = Iter.toArray(natSet.vals(s));
let t = Set.SetDebug.buildFromSorted(a);
Set.SetDebug.checkSetInvariants<Nat>(t, Nat.compare);
true
}),
prop("buildFromSorted(toArray(t)) == t", func (s) {
let a = Iter.toArray(natSet.vals(s));
let t = Set.SetDebug.buildFromSorted(a);
SetMatcher(s).matches(t)
})
]),

Expand Down
4 changes: 2 additions & 2 deletions test/OrderedSet.test.mo
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ let natSet = Set.Make<Nat>(Nat.compare);

func insert(s : Set.Set<Nat>, key : Nat) : Set.Set<Nat> {
let updatedTree = natSet.put(s, key);
Set.SetDebug.checkSetInvariants(updatedTree, Nat.compare);
natSet.validate(updatedTree);
updatedTree
};

Expand All @@ -51,7 +51,7 @@ func clear(initialRbSet : Set.Set<Nat>) : Set.Set<Nat> {
for (elem in natSet.vals(initialRbSet)) {
let newSet = natSet.delete(rbSet, elem);
rbSet := newSet;
Set.SetDebug.checkSetInvariants(rbSet, Nat.compare)
natSet.validate(rbSet)
};
rbSet
};
Expand Down

0 comments on commit 2119459

Please sign in to comment.