public void Test_Add_Remove_CopyTo_Keys_Values() { var table = new SlimHashTable <string, string>(x => x, 10); table.Add("foo"); AssertTableContents(table); var count = 32 * 5; for (var i = 0; i < count; i++) { table.Add("foo" + i); AssertTableContents(table); } AssertTableContents(table); Assert.IsTrue(table.Contains("foo")); Assert.AreEqual(count + 1, table.Count); Assert.IsFalse(table.Contains("bar")); AssertTableContents(table); for (var i = count - 1; i >= 0; i--) { var removed = table.Remove("foo" + i); Assert.IsTrue(removed); AssertTableContents(table); } Assert.IsTrue(table.Contains("foo")); Assert.AreEqual(1, table.Count); Assert.IsFalse(table.Contains("bar")); Assert.IsTrue(table.Remove("foo")); Assert.IsFalse(table.Contains("foo")); Assert.AreEqual(0, table.Count); }
public void TestAddException() { var table = new SlimHashTable <string, string>(x => x, 10); table.Add("10"); table.Add("10"); }
private FileDatabaseSnapshot CreateFileDatabase(FileSystemEntities entities, bool notifyProgress, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); using (new TimeElapsedLogger("Freezing file database state", cancellationToken)) { var progress = notifyProgress ? _progressTrackerFactory.CreateIndeterminateTracker() : null; try { progress?.DisplayProgress((i, n) => "Finalizing index update"); var directories = entities.Directories; // Note: We cannot use "ReferenceEqualityComparer<FileName>" here because // the dictionary will be used in incremental updates where FileName instances // may be new instances from a complete file system enumeration. var files = new SlimHashTable <FileName, FileWithContents>(v => v.FileName, entities.Files.Count); foreach (var kvp in entities.Files) { cancellationToken.ThrowIfCancellationRequested(); files.Add(kvp.Key, new FileWithContents(kvp.Value.FileName, kvp.Value.Contents)); } return(new FileDatabaseSnapshot(entities.ProjectHashes, directories, files)); } finally { progress?.Dispose(); } } }
public void TestGetHashCodeCalls() { var table = new SlimHashTable <MyTestType, MyTestType>(x => x, 10); var item = new MyTestType("test", 5); Assert.AreEqual(0, item.HashCodeCallCount); table.Add(item); Assert.AreEqual(1, item.HashCodeCallCount); // Add 100 elements to force a grow call for (var i = 0; i < 100; i++) { table.Add(new MyTestType(i.ToString(), i)); } // Assert Assert.AreEqual(1, item.HashCodeCallCount); }
public void TestUpdateValueType() { var table = new SlimHashTable <string, MyTestValueType>(x => x.Key, 10); table.Add(new MyTestValueType("test", 5)); Assert.IsTrue(table.Contains("test")); Assert.AreEqual(5, table["test"].Value); table["test"] = new MyTestValueType("test", 15); Assert.IsTrue(table.Contains("test")); Assert.AreEqual(15, table["test"].Value); }
private FileSystemEntities ComputeFileSystemEntities(FileSystemSnapshot snapshot, CancellationToken cancellationToken) { using (new TimeElapsedLogger("Computing tables of directory names and file names from FileSystemTree", cancellationToken)) { var directories = FileSystemSnapshotVisitor.GetDirectories(snapshot); //var directoryNames = new Dictionary<DirectoryName, DirectoryData>( var directoryNames = new SlimHashTable <DirectoryName, DirectoryData>( v => v.DirectoryName, directories.Count, // Note: We can use reference equality here because the directory // names are contructed unique. new ReferenceEqualityComparer <DirectoryName>()); foreach (var kvp in directories.ToForeachEnum()) { directoryNames.Add( kvp.Value.DirectoryName, new DirectoryData(kvp.Value.DirectoryName, kvp.Value.IsSymLink)); } var files = new SlimHashTable <FileName, ProjectFileData>( v => v.FileName, directories.Count * 2, // Note: We can use reference equality here because the file names are // constructed unique and the dictionary will be discarded once we are // done building this snapshot. new FileNameReferenceEqualityComparer()); foreach (var directory in directories.ToForeachEnum()) { foreach (var fileName in directory.Value.ChildFiles.ToForeachEnum()) { if (directory.Key.IsFileSearchable(fileName)) { files.Add(fileName, new ProjectFileData(directory.Key, fileName, null)); } } } return(new FileSystemEntities { Files = files, Directories = directoryNames, ProjectHashes = snapshot.ProjectRoots.ToDictionary(x => x.Project.RootPath, x => x.Project.VersionHash) }); } }