public void can_consume_an_added_page() { var subject = new FreeListPage(); subject.TryAdd(123); subject.TryAdd(234); var found = subject.TryGetNext(out var value); Assert.That(found, Is.True, "Lost free page"); Assert.That(value, Is.GreaterThanOrEqualTo(0), "Lost free page"); }
public void free_table_survives_serialisation() { var added = new List <int>(); var retrieved = new List <int>(); var original = new FreeListPage(); int i; for (i = 10; i < 100; i += i / 10) { Console.Write($"{i}, "); added.Add(i); original.TryAdd(i); } var bytes = original.Freeze(); var result = new FreeListPage(); result.Defrost(bytes); for (i = 0; i < 100; i++) { var ok = result.TryGetNext(out var free); if (ok && free > 0) { retrieved.Add(free); } else { break; } } Assert.That(retrieved, Is.EquivalentTo(added), "Free list was corrupted"); }
public void adding_a_negative_page_id_is_rejected() { var subject = new FreeListPage(); bool ok = subject.TryAdd(-1); Assert.That(ok, Is.False, "Add was accepted, but should have been rejected"); }
public void adding_a_page_to_a_list_with_space_is_accepted() { var subject = new FreeListPage(); bool ok = subject.TryAdd(123); Assert.That(ok, Is.True, "Add was rejected"); }
public void adding_a_page_to_a_full_list_is_rejected() { var subject = new FreeListPage(); int i; for (i = 0; i < FreeListPage.Capacity * 2; i++) { if (!subject.TryAdd(i + 4)) { break; } } Assert.That(i, Is.EqualTo(FreeListPage.Capacity), "Free list did not stop at limit"); }
public void adding_and_consuming_pages_out_of_sequence_works() { var expected = new HashSet <int>(); var rnd = new Random(); var subject = new FreeListPage(); int v; for (int i = 0; i < 1000; i++) { var q = rnd.Next(0, 2); switch (q) { case 0: v = rnd.Next(4, 50000); if (!expected.Contains(v) && subject.TryAdd(v)) { expected.Add(v); } Console.Write("+"); break; case 1: subject.TryGetNext(out v); if (v < 0) { break; } if (!expected.Contains(v)) { Assert.Fail("Unexpected value"); } expected.Remove(v); Console.Write("-"); break; } } }