damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

TrieTests.swift (2307B)


      1 //
      2 //  TrieTests.swift
      3 //  damusTests
      4 //
      5 //  Created by Terry Yiu on 6/26/23.
      6 //
      7 
      8 import XCTest
      9 @testable import damus
     10 
     11 final class TrieTests: XCTestCase {
     12 
     13     func testFind() throws {
     14         let trie = Trie<String>()
     15 
     16         let keys = ["foobar", "food", "foo", "somethingelse", "duplicate", "duplicate"]
     17         keys.forEach {
     18             trie.insert(key: $0, value: $0)
     19         }
     20 
     21         let allResults = trie.find(key: "")
     22         XCTAssertEqual(Set(allResults), Set(["foobar", "food", "foo", "somethingelse", "duplicate"]))
     23 
     24         let fooResults = trie.find(key: "foo")
     25         XCTAssertEqual(fooResults.first, "foo")
     26         XCTAssertEqual(Set(fooResults), Set(["foobar", "food", "foo"]))
     27 
     28         let foodResults = trie.find(key: "food")
     29         XCTAssertEqual(foodResults, ["food"])
     30 
     31         let ooResults = trie.find(key: "oo")
     32         XCTAssertEqual(Set(ooResults), Set(["foobar", "food", "foo"]))
     33 
     34         let aResults = trie.find(key: "a")
     35         XCTAssertEqual(Set(aResults), Set(["foobar", "duplicate"]))
     36 
     37         let notFoundResults = trie.find(key: "notfound")
     38         XCTAssertEqual(notFoundResults, [])
     39 
     40         // Sanity check that the root node has children.
     41         XCTAssertTrue(trie.hasChildren)
     42 
     43         // Sanity check that the root node has no values.
     44         XCTAssertFalse(trie.hasValues)
     45     }
     46 
     47     func testRemove() {
     48         let trie = Trie<String>()
     49 
     50         let keys = ["foobar", "food", "foo", "somethingelse", "duplicate", "duplicate"]
     51         keys.forEach {
     52             trie.insert(key: $0, value: $0)
     53         }
     54 
     55         keys.forEach {
     56             trie.remove(key: $0, value: $0)
     57         }
     58 
     59         let allResults = trie.find(key: "")
     60         XCTAssertTrue(allResults.isEmpty)
     61 
     62         let fooResults = trie.find(key: "foo")
     63         XCTAssertTrue(fooResults.isEmpty)
     64 
     65         let foodResults = trie.find(key: "food")
     66         XCTAssertTrue(foodResults.isEmpty)
     67 
     68         let ooResults = trie.find(key: "oo")
     69         XCTAssertTrue(ooResults.isEmpty)
     70 
     71         let aResults = trie.find(key: "a")
     72         XCTAssertTrue(aResults.isEmpty)
     73 
     74         // Verify that removal of values from all the keys that were inserted in the trie previously also resulted in the cleanup of the trie.
     75         XCTAssertFalse(trie.hasChildren)
     76         XCTAssertFalse(trie.hasValues)
     77     }
     78 
     79 }