2 // PatriciaTrieTests.cs
5 // Alexander Chebaturkin (chebaturkin@gmail.com)
7 // Copyright (C) 2012 Alexander Chebaturkin
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using Mono.CodeContracts.Static.DataStructures;
30 using Mono.CodeContracts.Static.DataStructures.Patricia;
32 using NUnit.Framework;
34 namespace MonoTests.Mono.CodeContracts {
36 public class PatriciaTrieTests {
37 readonly IImmutableIntMap<string> empty = ImmutableIntMap<string>.Empty;
40 public void AddOnEmptyShouldCreateLeafNode ()
42 var one = this.empty.Add (1, "hello") as LeafNode<string>;
44 Assert.That (one.Key, Is.EqualTo (1));
45 Assert.That (one.Value, Is.EqualTo ("hello"));
46 Assert.That (one.Count, Is.EqualTo (1));
50 public void AddOnLeafShouldCreateAnotherLeafIfKeysAreEqual ()
52 var leaf = new LeafNode<string> (1, "hello");
54 var result = (LeafNode<string>) leaf.Add (1, "there");
56 Assert.That (result.Key, Is.EqualTo (1));
57 Assert.That (result.Value, Is.EqualTo ("there"));
58 Assert.That (result.Count, Is.EqualTo (1));
62 public void AddOnLeafShouldCreateBranchIfKeysAreDifferent ()
64 var leaf = new LeafNode<string> (5, "hello"); //101
66 var branch = (BranchNode<string>) leaf.Add (7, "there"); //111
68 Assert.That (branch.Prefix, Is.EqualTo (1)); //x*1
69 Assert.That (branch.BranchingBit, Is.EqualTo (2)); //
70 Assert.That (branch.Left, Is.SameAs (leaf));
72 var right = (branch.Right as LeafNode<string>);
73 Assert.That (right, Is.Not.Null);
74 Assert.That (right.Key, Is.EqualTo (7));
75 Assert.That (right.Value, Is.EqualTo ("there"));
79 public void RemoveFromBranchWithLeafKeyEqualToArgumentShouldStayAnotherChild ()
81 IImmutableIntMap<string> left = this.empty.Add (5, "hello");
82 IImmutableIntMap<string> branch = left.Add (7, "there");
84 IImmutableIntMap<string> node1 = branch.Remove (7);
85 Assert.That (node1, Is.SameAs (left));
87 IImmutableIntMap<string> node2 = branch.Remove (5);
88 Assert.That (node2 is LeafNode<string>);
89 Assert.That ((node2 as LeafNode<string>).Key, Is.EqualTo (7));
90 Assert.That ((node2 as LeafNode<string>).Value, Is.EqualTo ("there"));
94 public void RemoveOnEmptyShouldStayEmpty ()
96 var one = (EmptyNode<string>) this.empty.Remove (1);
98 Assert.That (one, Is.SameAs (this.empty));
102 public void RemoveOnLeafShouldCreateEmptyIfKeysAreEqual ()
104 var leaf = new LeafNode<string> (1, "hello");
106 var result = (EmptyNode<string>) leaf.Remove (1);
108 Assert.That (result.Count, Is.EqualTo (0));
112 public void RemoveOnLeafShouldStayTheSameIfKeysAreDifferent ()
114 var leaf = new LeafNode<string> (1, "hello");
116 var result = (LeafNode<string>) leaf.Remove (2);
118 Assert.That (result, Is.SameAs (leaf));