public void MutableStringStore_Basic() { MutableStringStore store = new MutableStringStore(); // Add strings int systemID = store.FindOrAddString("System"); Assert.AreEqual(1, systemID, "Added strings get ascending integer IDs"); int collectionsID = store.FindOrAddString("Collections"); Assert.AreEqual(2, collectionsID, "Added strings get ascending integer IDs"); store.FindOrAddString("Generic"); store.FindOrAddString("List"); Assert.AreEqual(0, store.FindOrAddString(null), "Null and Empty always are index 0."); Assert.AreEqual(0, store.FindOrAddString(string.Empty), "Null and Empty always are index 0."); // Add an already-added string int secondCollectionsID = store.FindOrAddString("Collections"); Assert.AreEqual(collectionsID, secondCollectionsID); // Verify store is case sensitive [on add] int collectionsLowercaseID = store.FindOrAddString("collections"); Assert.AreNotEqual(collectionsID, collectionsLowercaseID); // Try finding an existing string int foundCollectionsID; bool isCollectionsFound = store.TryFindString("Collections", out foundCollectionsID); Assert.IsTrue(isCollectionsFound); Assert.AreEqual(collectionsID, foundCollectionsID); // Try finding a missing string int notFoundID; bool isMissingThingFound = store.TryFindString("Regex", out notFoundID); Assert.IsFalse(isMissingThingFound); // Test round-tripping to an ImmutableStringStore IStringStore loadedStore = Convert(store); // Confirm "Collections" was first (after String.Empty sentinel) Assert.AreEqual("Collections", loadedStore[1].ToString()); // Verify ImmutableStore finds both casings of Collections and returns the range Range matches; Assert.IsTrue(loadedStore.TryFindString("Collections", out matches)); Assert.AreEqual("1-2", matches.ToString()); }
public void ImmutableStringStore_Basic() { // Set of strings to index [out of order to verify GetSerializationIdentifier] string[] strings = { "Boolean", "Abacus", "Array", "ArrayList", "Editor", "Collections", "Dictionary" }; int[] identifiers = new int[strings.Length]; // Add values to a mutable store and track identifiers MutableStringStore store = new MutableStringStore(); for (int i = 0; i < strings.Length; ++i) { identifiers[i] = store.FindOrAddString(strings[i]); } // Convert to immutable IStringStore iStore = Convert(store, identifiers); // Verify each value is found at the expected position byte[] buffer = new byte[32]; for (int i = 0; i < strings.Length; ++i) { String8 value = String8.Convert(strings[i], buffer); Range foundAtIndex; Assert.IsTrue(iStore.TryFindString(value, out foundAtIndex), "ImmutableStore didn't contain added value \"{0}\"", strings[i]); Assert.AreEqual(identifiers[i], foundAtIndex.Start, "ImmutableStore didn't find value at SerializationIdentifier position"); Assert.AreEqual(value, iStore[foundAtIndex.Start], "ImmutableStore didn't rebuild string with same value"); } // Verify values not in collection aren't found and (single) insertion positions are returned Range matches; Assert.IsFalse(iStore.TryFindString("ZZ AfterLastValue", out matches)); Assert.AreEqual((FindIdentifier("Editor", strings, identifiers) + 1).ToString(), matches.ToString()); Assert.IsFalse(iStore.TryFindString("AA BeforeFirstValue", out matches)); Assert.AreEqual(FindIdentifier("Abacus", strings, identifiers).ToString(), matches.ToString()); Assert.IsFalse(iStore.TryFindString("Bz Between Boolean and Collections", out matches)); Assert.AreEqual(FindIdentifier("Collections", strings, identifiers).ToString(), matches.ToString()); Assert.IsFalse(iStore.TryFindString("Cz Between Collections and Dictionary", out matches)); Assert.AreEqual(FindIdentifier("Dictionary", strings, identifiers).ToString(), matches.ToString()); // Check range searches Assert.AreEqual("Array-ArrayList", GetRangeValuesAsString("Arr", iStore), "Prefix covering multiple items should match all"); Assert.AreEqual("Array-ArrayList", GetRangeValuesAsString("Array", iStore), "Prefix equalling an item should include it"); Assert.AreEqual("ArrayList", GetRangeValuesAsString("ArrayL", iStore), "Prefix of only one item has only it"); Assert.AreEqual(string.Empty, GetRangeValuesAsString("ArrayList2", iStore), "Prefix longer than item should not match"); Assert.AreEqual("Abacus-ArrayList", GetRangeValuesAsString("A", iStore), "Prefix containing first item should include it"); Assert.AreEqual("Editor", GetRangeValuesAsString("Edit", iStore), "Prefix containing last item should include it"); Assert.AreEqual(string.Empty, GetRangeValuesAsString("AA", iStore), "Prefix before first item has empty range"); Assert.AreEqual(string.Empty, GetRangeValuesAsString("ZZ", iStore), "Prefix after last item has empty range"); }