|
| 1 | +import 'package:test/test.dart'; |
| 2 | +import 'package:algorithms/trie/trie.dart'; |
| 3 | + |
| 4 | +void main() { |
| 5 | + Trie emptyTrie, trie, customTrie; |
| 6 | + |
| 7 | + setUp(() { |
| 8 | + emptyTrie = Trie.ofAlphabets(); |
| 9 | + |
| 10 | + trie = Trie.ofAlphabets(); |
| 11 | + trie.add('algorithms'); |
| 12 | + |
| 13 | + customTrie = Trie( |
| 14 | + {'a', 'b', 'f', 'o', 'r', 'z'}, (value) => value.toString().split('')); |
| 15 | + customTrie.add('foo'); |
| 16 | + }); |
| 17 | + |
| 18 | + test('Components', () { |
| 19 | + expect(trie.components, |
| 20 | + equals({...List.generate(26, (i) => String.fromCharCode(122 - i))})); |
| 21 | + |
| 22 | + expect(customTrie.components, equals({'f', 'o', 'b', 'a', 'r', 'z'})); |
| 23 | + }); |
| 24 | + |
| 25 | + test('Empty trie', () { |
| 26 | + expect(emptyTrie.isEmpty, isTrue); |
| 27 | + expect(trie.isEmpty, isFalse); |
| 28 | + }); |
| 29 | + |
| 30 | + test('Add value', () { |
| 31 | + trie.add('hi'); |
| 32 | + expect(trie.root.children[7].children[8].isValue, isTrue); |
| 33 | + |
| 34 | + customTrie.add('bar'); |
| 35 | + expect(customTrie.root.children[1].children[0].children[4].isValue, isTrue); |
| 36 | + |
| 37 | + customTrie.add('baz'); |
| 38 | + expect(customTrie.root.children[1].children[0].children[5].isValue, isTrue); |
| 39 | + }); |
| 40 | + |
| 41 | + test('Contains value', () { |
| 42 | + expect(emptyTrie.contains('test'), isFalse); |
| 43 | + |
| 44 | + expect(trie.contains('algorithm'), isFalse); |
| 45 | + expect(trie.contains('algorithms'), isTrue); |
| 46 | + |
| 47 | + expect(customTrie.contains('boar'), isFalse); |
| 48 | + expect(customTrie.contains('foo'), isTrue); |
| 49 | + }); |
| 50 | + |
| 51 | + test('Delete value', () { |
| 52 | + emptyTrie.delete(''); |
| 53 | + emptyTrie.delete('test'); |
| 54 | + |
| 55 | + expect(trie.contains('algorithms'), isTrue); |
| 56 | + trie.delete('algorithms'); |
| 57 | + expect(trie.contains('algorithms'), isFalse); |
| 58 | + |
| 59 | + expect(customTrie.contains('foo'), isTrue); |
| 60 | + customTrie.delete('foo'); |
| 61 | + expect(customTrie.contains('foo'), isFalse); |
| 62 | + }); |
| 63 | +} |
0 commit comments