Skip to content

Commit

Permalink
Add order bounds on BTree.init()
Browse files Browse the repository at this point in the history
  • Loading branch information
ByronBecker committed Feb 5, 2023
1 parent 0f6e2af commit ddf295f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/BTree.mo
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ module {
public type Leaf<K, V> = Types.Leaf<K, V>;
public type Data<K, V> = Types.Data<K, V>;

// TODO - enforce BTrees to have order of at least 4
// Initializes an empty BTree. By default, set the BTree to have order 8, and enforce the the order be greater than 4, but lower than 512
public func init<K, V>(order: ?Nat): BTree<K, V> {
let btreeOrder = switch(order) {
case null { 8 };
case (?providedOrder) { providedOrder };
case (?providedOrder) {
if (providedOrder < 4) { Debug.trap("provided order=" # Nat.toText(providedOrder) # ", but Btree order must be >= 4 and <= 512") };
if (providedOrder > 512) { Debug.trap("provided order=" # Nat.toText(providedOrder) # ", but Btree order must be >= 4 and <= 512") };
providedOrder
};
};

{
Expand Down
28 changes: 28 additions & 0 deletions test/BTreeTest.mo
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,35 @@ let initSuite = S.suite("init", [
var size = 0;
order = 8;
}))
),
/* Comment out to test that these tests trap on BTree initialization
S.test("if the order provided is less than 4, traps",
BT.init<Nat, Nat>(?3),
M.equals(testableNatBTree({
var root = #leaf({
data = {
kvs = [var null, null];
var count = 0;
}
});
var size = 0;
order = 3;
}))
),
S.test("if the order provided is greater than 512, traps",
BT.init<Nat, Nat>(?513),
M.equals(testableNatBTree({
var root = #leaf({
data = {
kvs = [var null, null];
var count = 0;
}
});
var size = 0;
order = 512;
}))
)
*/
]);

let getSuite = S.suite("get", [
Expand Down

0 comments on commit ddf295f

Please sign in to comment.