public void can_use__Or__predicates_failure_case() { var result = Check.That(subject).singleValue[Should.Equal("wrong").Or(Should.Equal("wronger"))]; Assert.That(result.Success, Is.False); Assert.That(result.Reasons, Contains.Item("Outer.singleValue is not equal to wrong, is not equal to wronger")); }
static void TestRandomAllocFree() { var pool = stackalloc Pool[1]; Init(pool); const int len = 200; var ptrs = new System.Collections.Generic.List <long>(); for (int i = 0; i < len; i += 1) { int size = Fdb.Random(1, 1000); ptrs.Add((long)Pool.Alloc(pool, size)); if (pool->shift != 0) { long shift = pool->shift; for (int j = 0; j < ptrs.Count - 1; j += 1) { ptrs[j] += shift; } pool->shift = 0; } if (Fdb.Random(0, 2) == 0) // 1/3 chance to free { int idx = Fdb.Random(0, ptrs.Count - 1); Pool.Free(pool, (void *)ptrs[idx]); ptrs.RemoveAt(idx); } } for (int i = 0; i < ptrs.Count; i += 1) { Pool.Free(pool, (void *)ptrs[i]); } // the first *real* free node should be 0x54 long (containing all space) Should.Equal("*(int *)(pool->arr + 0x28)", *(int *)(pool->arr + 0x18), pool->len - 11 * 4); }
static void TestPushRemovePush() { const int len = 1000; var arr = new System.Collections.Generic.List <int>(); var lst = stackalloc PtrLst[1]; Init(lst); for (int i = 0; i < len; i += 1) { int v = Fdb.Random(0, len); arr.Add(v); Push(lst, (void *)v); } for (int i = 0; i < len >> 1; i += 1) { int idx = Fdb.Random(0, arr.Count); arr.RemoveAt(idx); RemoveAt(lst, idx); } for (int i = 0; i < len; i += 1) { int v = Fdb.Random(0, len); arr.Add(v); Push(lst, (void *)v); } void **ptr = lst->arr; for (int i = 0; i < arr.Count; i += 1) { Should.Equal("(int)ptr[i]", (int)ptr[i], arr[i]); } }
static void TestRandomExpand() { var pool = stackalloc Pool[1]; Init(pool); const int len = 200; var ptrs = stackalloc byte *[len]; for (int i = 0; i < len; i += 1) { int size = Fdb.Random(1, 1000); ptrs[i] = (byte *)Pool.Alloc(pool, size); if (pool->shift != 0) { long shift = pool->shift; for (int j = 0; j < i; j += 1) { ptrs[j] += shift; } pool->shift = 0; } } for (int i = 0; i < len; i += 1) { Pool.Free(pool, ptrs[i]); } // the first *real* free node should be 0x54 long (containing all space) Should.Equal("*(int *)(pool->arr + 0x28)", *(int *)(pool->arr + 0x18), pool->len - 11 * 4); }
public static void Remove(PtrLst *self, void *ptr) { #if FDB Should.NotNull("self", self); Should.TypeEqual("self", self->type, Type); int oldCount = self->count; #endif void **arr = self->arr; int i = 0, len = self->count; while (i < len && arr[i] != ptr) { i += 1; } if (i < len) // arr[i] == p // for (len = self->count -= 1; i < len; i += 1) arr[i] = arr[i + 1]; { byte *src = (byte *)(arr + i); int size = self->size; int count = self->count -= 1; Mem.Memmove(src, src + size, (count - i) * size); } #if FDB else { Fdb.Error("{0:X} does not exist in PtrLst {1:X}", (ulong)ptr, (ulong)self); } Should.Equal("self->count", self->count, oldCount - 1); #endif }
public void can_check_against_null_member() { subject.Two.X.Value = null; var result = Check.That(subject).Two.X.Value[Should.Equal(true)]; Assert.That(result.Success, Is.False); Assert.That(result.Reasons, Contains.Item("BaseThing.Two.X.Value is not equal to True")); }
public void fails_if_any_assertion_fails() { Check.Result result = Check.That(subject).Two.X[Should.Be <A>(), Should.Equal(subject.Two.X), Should.BeNull]; Console.WriteLine(string.Join(" ", result.Reasons)); Assert.That(result.Success, Is.False); }
static void TestBasic() { var dict = stackalloc PtrIntDict[1]; Init(dict); Set(dict, 1, (void *)1); Should.Equal("(int)Get(dict, 1)", (int)Get(dict, 1), 1); Remove(dict, 1); Should.Null("Get(dict, 1)", Get(dict, 1)); }
public static void Verify(PtrLst *self) { Should.NotNull("self", self); Should.TypeEqual("self", self->type, Type); int len = Mem.Verify(self->arr) / sizeof(void *); Should.Equal("self->len", self->len, len); Should.InRange("self->count", self->count, 0, self->len); Should.Equal("self->size", self->size, sizeof(void *)); }
public void can_run_the_same_assertions_on_multiple_stems_OR_case() { var stem1 = Check.That(subject).one; var stem2 = Check.That(subject).two; var result = Check.AtLeastOne(new [] { stem1, stem2 }, two => two.three.check[Should.Equal("Hello")] ); Assert.That(result.Success, Is.True); }
public void can_run_the_same_assertions_on_multiple_stems() { Check stem1 = Check.That(subject).one.two; Check stem2 = Check.That(subject).two; var result = Check.With(new [] { stem1, stem2 }, two => two.three.check[Should.Equal("Hello")] ); Assert.That(result.Success, Is.True, result.Reason); }
static void TestSetGet() { var dict = stackalloc PtrIntDict[1]; Init(dict); for (int i = 2; i < 100; i += 1) { Set(dict, i, (void *)i); for (int j = 2; j <= i; j += 1) { Should.Equal("Get(dict, j)", (int)Get(dict, j), j); } } }
public void can_run_the_same_assertions_on_multiple_stems_OR_failure_case() { var stem1 = Check.That(subject).one; var stem2 = Check.That(subject).two; var result = Check.AtLeastOne(new [] { stem1, stem2 }, two => two.three.check[Should.Equal("World")] ); Assert.That(result.Success, Is.False); Assert.That(result.Reasons, Contains.Item("Root.one.three is not a valid path")); Assert.That(result.Reasons, Contains.Item("Root.two.three.check is not equal to World")); }
public static void Verify(PtrIntDict *self) { Should.NotNull("self", self); Should.InRange("self->level", self->level, 0, Const.Lens.Length - 1); Should.Equal("self->len", self->len, Const.Lens[self->level]); Should.InRange("self->count", self->count, 0, self->len); Should.InRange("self->free", self->free, -1, self->len - 1); Mem.Verify(self->entries); Should.NotNull("self->arr", self->arr); int len = self->len; int count = self->count; int j = 0; for (int i = 0; i < len; i += 1) { var entry = self->entries + i; if (entry->val != null) // a real key { j += 1; } } Should.Equal("j", j, count); int freeCount = len - count; int free = self->free; while (free != -1) { freeCount -= 1; free = self->entries[free].next; } Should.Zero("freeCount", freeCount); j = 0; for (int i = 0; i < len; i += 1) { int idx = self->arr[i]; while (idx != -1) { var entry = self->entries + idx; Should.Equal("entry->hash % len", (uint)entry->hash % len, i); j += 1; idx = entry->next; } } // Fdb.Dump(self->arr, self->len * IntSize); // Fdb.Dump(self->entries, self->len * EntrySize, EntrySize); Should.Equal("j", j, count); }
public void should_be_able_to_assert_multiple_routes_from_one_stem() { var stem = Check.That(subject).a.deep.way.down; var r1 = stem.further[Should.NotBeNull]; var r2 = stem.different.Path[Should.NotBeEmpty]; var r3 = stem.different.Path[Should.Equal("Howdy")]; Assert.That(r1.Success, Is.True, r1.Reason); Assert.That(r2.Success, Is.True, r2.Reason); Assert.That(r3.Success, Is.False); Assert.That(r3.Reason, Is.StringContaining(".a.deep.way.down.different.Path is not equal to Howdy")); }
static void TestBinarySearch() { const int len = 100; var arr = stackalloc void *[len]; for (int i = 0; i < len; i += 1) { arr[i] = (void *)i; } for (int i = 0; i < len; i += 1) { int res = BinarySearch(arr, len, (void *)i, (a, b) => (int)a - (int)b); Should.Equal("res", res, i); } }
static void TestPush() { const int len = 1000; var arr = stackalloc int[len]; var lst = stackalloc PtrLst[1]; Init(lst); for (int i = 0; i < len; i += 1) { arr[i] = Fdb.Random(0, len); Push(lst, (void *)arr[i]); } void **ptr = lst->arr; for (int i = 0; i < len; i += 1) { Should.Equal("(int)ptr[i]", (int)ptr[i], arr[i]); } }
static void TestExpand1() { var pool = stackalloc Pool[1]; Init(pool); var p = Pool.Alloc(pool, 4); *(int *)p = 0x4a4a4a4a; Pool.Free(pool, p); p = Pool.Alloc(pool, 4); *(int *)p = 0x4a4a4a4a; var p2 = Pool.Alloc(pool, 4); *(int *)p2 = 0x4a4a4a4a; Pool.Free(pool, p2); p = (void *)((byte *)p + pool->shift); Pool.Free(pool, p); // the first *real* free node should be 0x54 long (containing all space) Should.Equal("*(int *)(pool->arr + 0x28)", *(int *)(pool->arr + 0x18), pool->len - 11 * 4); }
public void should_be_able_to_group_assert_on_a_stem() { var stem = Check.That(subject).a.deep.way.down; var pass = Check.With((Check)stem, s => s.further[Should.NotBeNull], s => s.different.Path[Should.NotBeEmpty] ); var fail = Check.With((Check)stem, s => s.different.Path[Should.Equal("Howdy")] ); Assert.That(pass.Success, Is.True, pass.Reason); Assert.That(fail.Success, Is.False); Assert.That(fail.Reason, Is.StringContaining(".a.deep.way.down.different.Path is not equal to Howdy")); }
static void TestShiftPtr() { const int len = 1000; var arr = stackalloc int[len]; var lst = stackalloc PtrLst[1]; Init(lst); for (int i = 0; i < len; i += 1) { arr[i] = Fdb.Random(0, len); Push(lst, (void *)arr[i]); } long shift = Fdb.Random(-1000, 1000); ShiftBase(lst, shift); void **ptr = lst->arr; for (int i = 0; i < len; i += 1) { Should.Equal("(int)ptr[i]", (long)ptr[i], arr[i] + shift); } }
static void TestQuickSelect() { const int len = 100; var arr = stackalloc void *[len]; for (int i = 0; i < len; i += 1) { arr[i] = (void *)i; } for (int i = len - 1; i >= 0; i -= 1) { int k = Fdb.Random(0, i); void *swap = arr[k]; arr[k] = arr[i]; arr[i] = swap; } for (int i = 0; i < len; i += 1) { void *res = QuickSelect(arr, len, i, (a, b) => (int)a - (int)b); Should.Equal("res", res, (void *)i); } }
static void *QuickSelect(void **arr, int low, int high, int k, Cmp cmp) { #if FDB if (low >= high) { Should.Equal("low", low, high); Should.Equal("k", k, low); } #endif if (low >= high) { return(arr[low]); } void *pivot = arr[low]; void *swap; int mid = low; for (int j = low + 1; j <= high; j += 1) { if (cmp(swap = arr[j], pivot) < 0) { mid += 1; arr[j] = arr[mid]; arr[mid] = swap; } } if (mid != low) { swap = arr[mid]; arr[mid] = arr[low]; arr[low] = swap; } if (mid == k) { return(arr[mid]); } return(k < mid?QuickSelect(arr, low, mid - 1, k, cmp) : QuickSelect(arr, mid + 1, high, k, cmp)); }
static void TestRandomSetGetRemove() { var dict = stackalloc PtrIntDict[1]; Init(dict); var keyList = new System.Collections.Generic.List <int>(); var valList = new System.Collections.Generic.List <int>(); for (int i = 2; i < 100; i += 1) { int key = Fdb.Random(-0x8000000, 0x7fffffff); int val = Fdb.Random(-0x8000000, 0x7fffffff); if (val == 0) { val = 1; } Set(dict, key, (void *)val); keyList.Add(key); valList.Add(val); for (int j = 0; j < keyList.Count; j += 1) { Should.Equal("Get(dict, j)", (int)Get(dict, keyList[j]), valList[j]); } } for (int i = 0; i < keyList.Count; i += 1) { Remove(dict, keyList[i]); for (int j = 0; j < keyList.Count; j += 1) { if (j <= i) { Should.Null("Get(dict, j)", Get(dict, keyList[j])); } else { Should.Equal("Get(dict, j)", (int)Get(dict, keyList[j]), valList[j]); } } } }
static void TestSetGetRemove() { var dict = stackalloc PtrIntDict[1]; Init(dict); for (int i = 2; i < 100; i += 1) { Set(dict, i, (void *)i); } for (int i = 2; i < 100; i += 1) { Remove(dict, i); for (int j = 2; j < 100; j += 1) { if (j <= i) { Should.Null("Get(dict, j)", Get(dict, j)); } else { Should.Equal("Get(dict, j)", (int)Get(dict, j), j); } } } }
public static void Verify(Pool *self) { Should.NotNull("self", self); Should.TypeEqual("self", self->type, Type); Should.GreaterThanZero("self->len", self->len); Should.Equal("self->HeadSize", HeadSize, 3 * sizeof(int)); Should.Equal("self->TailSize", TailSize, sizeof(int)); int len = Mem.Verify(self->arr); Should.Equal("self->len", self->len, len); // Fdb.Dump(self->arr, len); byte *arr = self->arr; int * head; head = (int *)(arr + self->len - HeadSize); Should.Equal("endHead->prev", head[0], -1); // head.prev = -1 Should.Equal("endHead->next", head[1], -1); // head.next = -1 Should.Equal("endHead->size", head[2], 0); // head.size = 0 head = (int *)arr; Should.Equal("firstHead->prev", head[0], -1); // head.prev = -1 Should.Equal("firstHead->size", head[2], 0); // head.size = 0 Should.Equal("firstTail->size", head[3], -1); // tail.size = -1 int curFree = head[1], lastFree = 0; var dict = new System.Collections.Generic.Dictionary <int, int>(); while (curFree != -1) { head = (int *)(arr + curFree); Should.Equal("head" + curFree + "->prev", head[0], lastFree); Should.Equal("tail->size", *(int *)((byte *)head + HeadSize + head[2]), head[2]); dict.Add(curFree, head[2]); lastFree = curFree; curFree = head[1]; } head = (int *)(arr + HeadSize + TailSize); int *end = (int *)(arr + self->len); while (head < end) { int pos = (int)((byte *)head - self->arr); int prev = head[0], next = head[1], size = head[2]; int *tail = (int *)((byte *)head + HeadSize + size); if (tail < end) { int tailVal = tail[0]; if (tailVal == -1) // in use { Should.False("dict.ContainsKey(pos)", dict.ContainsKey(pos)); Should.Equal("prev", prev, -1); Should.Equal("next", next, -1); Should.GreaterThanZero("size", size); } else // free { Should.True("dict.ContainsKey(pos)" + pos, dict.ContainsKey(pos)); Should.GreaterThanOrEqualTo("prev", prev, 0); Should.GreaterThanOrEqualTo("next", next, -1); Should.Equal("size", size, dict[pos]); dict.Remove(pos); } } else // head is end sentinel, no tail { Should.Equal("head", head, (int *)(arr + len - HeadSize)); Should.Equal("endHead->prev", head[0], -1); // head.prev = -1 Should.Equal("endHead->next", head[1], -1); // head.next = -1 Should.Equal("endHead->size", head[2], 0); // head.size = 0 } head = (int *)((byte *)head + HeadSize + size + TailSize); } Should.Zero("dict.Count", dict.Count); }
public void can_check_that_nth_item_validates(int n, bool a_value) { var result = Check.That(subject).container(n).a[Should.Equal(a_value)]; Assert.That(result.Success, Is.True, string.Join(" ", result.Reasons)); }
public void can_use__Or__predicates() { var result = Check.That(subject).singleValue[Should.Equal("enter").Or(Should.Equal("exit"))]; Assert.That(result.Success, Is.True, result.Reason); }
public void can_assert_that_nth_child_validates() { var result = Check.That(subject).SingleThing.ListOfK(2).Value[Should.Equal("Hello, Alice")]; Assert.That(result.Success, Is.True, result.Reason); }
public void can_use_more_than_one_assertion() { Check.Result result = Check.That(subject).Two.X[Should.Be <A>(), Should.Equal(subject.Two.X)]; Assert.That(result.Success, Is.True); }