You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by nw...@apache.org on 2013/05/30 22:36:26 UTC
[lucy-commits] [08/26] Move Clownfish runtime tests to a separate parcel
http://git-wip-us.apache.org/repos/asf/lucy/blob/9f826b31/core/Clownfish/Test/TestVArray.c
----------------------------------------------------------------------
diff --git a/core/Clownfish/Test/TestVArray.c b/core/Clownfish/Test/TestVArray.c
new file mode 100644
index 0000000..99b5918
--- /dev/null
+++ b/core/Clownfish/Test/TestVArray.c
@@ -0,0 +1,353 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string.h>
+
+#define CFISH_USE_SHORT_NAMES
+#define TESTCFISH_USE_SHORT_NAMES
+
+#include "Clownfish/Test/TestVArray.h"
+
+#include "Clownfish/CharBuf.h"
+#include "Clownfish/Err.h"
+#include "Clownfish/Num.h"
+#include "Clownfish/Test.h"
+#include "Clownfish/Test/TestFormatter.h"
+#include "Clownfish/Test/TestUtils.h"
+#include "Clownfish/VArray.h"
+#include "Clownfish/VTable.h"
+
+TestVArray*
+TestVArray_new(TestFormatter *formatter) {
+ TestVArray *self = (TestVArray*)VTable_Make_Obj(TESTVARRAY);
+ return TestVArray_init(self, formatter);
+}
+
+TestVArray*
+TestVArray_init(TestVArray *self, TestFormatter *formatter) {
+ return (TestVArray*)TestBatch_init((TestBatch*)self, 45, formatter);
+}
+
+static CharBuf*
+S_new_cb(const char *text) {
+ return CB_new_from_utf8(text, strlen(text));
+}
+
+static void
+test_Equals(TestBatch *batch) {
+ VArray *array = VA_new(0);
+ VArray *other = VA_new(0);
+ ZombieCharBuf *stuff = ZCB_WRAP_STR("stuff", 5);
+
+ TEST_TRUE(batch, VA_Equals(array, (Obj*)other),
+ "Empty arrays are equal");
+
+ VA_Push(array, (Obj*)CFISH_TRUE);
+ TEST_FALSE(batch, VA_Equals(array, (Obj*)other),
+ "Add one elem and Equals returns false");
+
+ VA_Push(other, (Obj*)CFISH_TRUE);
+ TEST_TRUE(batch, VA_Equals(array, (Obj*)other),
+ "Add a matching elem and Equals returns true");
+
+ VA_Store(array, 2, (Obj*)CFISH_TRUE);
+ TEST_FALSE(batch, VA_Equals(array, (Obj*)other),
+ "Add elem after a NULL and Equals returns false");
+
+ VA_Store(other, 2, (Obj*)CFISH_TRUE);
+ TEST_TRUE(batch, VA_Equals(array, (Obj*)other),
+ "Empty elems don't spoil Equals");
+
+ VA_Store(other, 2, INCREF(stuff));
+ TEST_FALSE(batch, VA_Equals(array, (Obj*)other),
+ "Non-matching value spoils Equals");
+
+ VA_Excise(array, 1, 2); // removes empty elems
+ VA_Delete(other, 1); // leaves NULL in place of deleted elem
+ VA_Delete(other, 2);
+ TEST_FALSE(batch, VA_Equals(array, (Obj*)other),
+ "Empty trailing elements spoil Equals");
+
+ DECREF(array);
+ DECREF(other);
+}
+
+static void
+test_Store_Fetch(TestBatch *batch) {
+ VArray *array = VA_new(0);
+ CharBuf *elem;
+
+ TEST_TRUE(batch, VA_Fetch(array, 2) == NULL, "Fetch beyond end");
+
+ VA_Store(array, 2, (Obj*)CB_newf("foo"));
+ elem = (CharBuf*)CERTIFY(VA_Fetch(array, 2), CHARBUF);
+ TEST_INT_EQ(batch, 3, VA_Get_Size(array), "Store updates size");
+ TEST_TRUE(batch, CB_Equals_Str(elem, "foo", 3), "Store");
+
+ INCREF(elem);
+ TEST_INT_EQ(batch, 2, CB_Get_RefCount(elem),
+ "start with refcount of 2");
+ VA_Store(array, 2, (Obj*)CB_newf("bar"));
+ TEST_INT_EQ(batch, 1, CB_Get_RefCount(elem),
+ "Displacing elem via Store updates refcount");
+ DECREF(elem);
+ elem = (CharBuf*)CERTIFY(VA_Fetch(array, 2), CHARBUF);
+ TEST_TRUE(batch, CB_Equals_Str(elem, "bar", 3), "Store displacement");
+
+ DECREF(array);
+}
+
+static void
+test_Push_Pop_Shift_Unshift(TestBatch *batch) {
+ VArray *array = VA_new(0);
+ CharBuf *elem;
+
+ TEST_INT_EQ(batch, VA_Get_Size(array), 0, "size starts at 0");
+ VA_Push(array, (Obj*)CB_newf("a"));
+ VA_Push(array, (Obj*)CB_newf("b"));
+ VA_Push(array, (Obj*)CB_newf("c"));
+
+ TEST_INT_EQ(batch, VA_Get_Size(array), 3, "size after Push");
+ TEST_TRUE(batch, NULL != CERTIFY(VA_Fetch(array, 2), CHARBUF), "Push");
+
+ elem = (CharBuf*)CERTIFY(VA_Shift(array), CHARBUF);
+ TEST_TRUE(batch, CB_Equals_Str(elem, "a", 1), "Shift");
+ TEST_INT_EQ(batch, VA_Get_Size(array), 2, "size after Shift");
+ DECREF(elem);
+
+ elem = (CharBuf*)CERTIFY(VA_Pop(array), CHARBUF);
+ TEST_TRUE(batch, CB_Equals_Str(elem, "c", 1), "Pop");
+ TEST_INT_EQ(batch, VA_Get_Size(array), 1, "size after Pop");
+ DECREF(elem);
+
+ VA_Unshift(array, (Obj*)CB_newf("foo"));
+ elem = (CharBuf*)CERTIFY(VA_Fetch(array, 0), CHARBUF);
+ TEST_TRUE(batch, CB_Equals_Str(elem, "foo", 3), "Unshift");
+ TEST_INT_EQ(batch, VA_Get_Size(array), 2, "size after Shift");
+
+ DECREF(array);
+}
+
+static void
+test_Delete(TestBatch *batch) {
+ VArray *wanted = VA_new(5);
+ VArray *got = VA_new(5);
+ uint32_t i;
+
+ for (i = 0; i < 5; i++) { VA_Push(got, (Obj*)CB_newf("%u32", i)); }
+ VA_Store(wanted, 0, (Obj*)CB_newf("0", i));
+ VA_Store(wanted, 1, (Obj*)CB_newf("1", i));
+ VA_Store(wanted, 4, (Obj*)CB_newf("4", i));
+ DECREF(VA_Delete(got, 2));
+ DECREF(VA_Delete(got, 3));
+ TEST_TRUE(batch, VA_Equals(wanted, (Obj*)got), "Delete");
+
+ DECREF(wanted);
+ DECREF(got);
+}
+
+static void
+test_Resize(TestBatch *batch) {
+ VArray *array = VA_new(3);
+ uint32_t i;
+
+ for (i = 0; i < 2; i++) { VA_Push(array, (Obj*)CB_newf("%u32", i)); }
+ TEST_INT_EQ(batch, VA_Get_Capacity(array), 3, "Start with capacity 3");
+
+ VA_Resize(array, 4);
+ TEST_INT_EQ(batch, VA_Get_Size(array), 4, "Resize up");
+ TEST_INT_EQ(batch, VA_Get_Capacity(array), 4,
+ "Resize changes capacity");
+
+ VA_Resize(array, 2);
+ TEST_INT_EQ(batch, VA_Get_Size(array), 2, "Resize down");
+ TEST_TRUE(batch, VA_Fetch(array, 2) == NULL, "Resize down zaps elem");
+
+ DECREF(array);
+}
+
+static void
+test_Excise(TestBatch *batch) {
+ VArray *wanted = VA_new(5);
+ VArray *got = VA_new(5);
+
+ for (uint32_t i = 0; i < 5; i++) {
+ VA_Push(wanted, (Obj*)CB_newf("%u32", i));
+ VA_Push(got, (Obj*)CB_newf("%u32", i));
+ }
+
+ VA_Excise(got, 7, 1);
+ TEST_TRUE(batch, VA_Equals(wanted, (Obj*)got),
+ "Excise outside of range is no-op");
+
+ VA_Excise(got, 2, 2);
+ DECREF(VA_Delete(wanted, 2));
+ DECREF(VA_Delete(wanted, 3));
+ VA_Store(wanted, 2, VA_Delete(wanted, 4));
+ VA_Resize(wanted, 3);
+ TEST_TRUE(batch, VA_Equals(wanted, (Obj*)got),
+ "Excise multiple elems");
+
+ VA_Excise(got, 2, 2);
+ VA_Resize(wanted, 2);
+ TEST_TRUE(batch, VA_Equals(wanted, (Obj*)got),
+ "Splicing too many elems truncates");
+
+ VA_Excise(got, 0, 1);
+ VA_Store(wanted, 0, VA_Delete(wanted, 1));
+ VA_Resize(wanted, 1);
+ TEST_TRUE(batch, VA_Equals(wanted, (Obj*)got),
+ "Excise first elem");
+
+ DECREF(got);
+ DECREF(wanted);
+}
+
+static void
+test_Push_VArray(TestBatch *batch) {
+ VArray *wanted = VA_new(0);
+ VArray *got = VA_new(0);
+ VArray *scratch = VA_new(0);
+ uint32_t i;
+
+ for (i = 0; i < 4; i++) { VA_Push(wanted, (Obj*)CB_newf("%u32", i)); }
+ for (i = 0; i < 2; i++) { VA_Push(got, (Obj*)CB_newf("%u32", i)); }
+ for (i = 2; i < 4; i++) { VA_Push(scratch, (Obj*)CB_newf("%u32", i)); }
+
+ VA_Push_VArray(got, scratch);
+ TEST_TRUE(batch, VA_Equals(wanted, (Obj*)got), "Push_VArray");
+
+ DECREF(wanted);
+ DECREF(got);
+ DECREF(scratch);
+}
+
+static void
+test_Slice(TestBatch *batch) {
+ VArray *array = VA_new(0);
+ for (uint32_t i = 0; i < 10; i++) { VA_Push(array, (Obj*)CB_newf("%u32", i)); }
+ {
+ VArray *slice = VA_Slice(array, 0, 10);
+ TEST_TRUE(batch, VA_Equals(array, (Obj*)slice), "Slice entire array");
+ DECREF(slice);
+ }
+ {
+ VArray *slice = VA_Slice(array, 0, 11);
+ TEST_TRUE(batch, VA_Equals(array, (Obj*)slice),
+ "Exceed length");
+ DECREF(slice);
+ }
+ {
+ VArray *wanted = VA_new(0);
+ VA_Push(wanted, (Obj*)CB_newf("9"));
+ VArray *slice = VA_Slice(array, 9, 11);
+ TEST_TRUE(batch, VA_Equals(slice, (Obj*)wanted),
+ "Exceed length, start near end");
+ DECREF(slice);
+ DECREF(wanted);
+ }
+ {
+ VArray *slice = VA_Slice(array, 0, 0);
+ TEST_TRUE(batch, VA_Get_Size(slice) == 0, "empty slice");
+ DECREF(slice);
+ }
+ {
+ VArray *slice = VA_Slice(array, 20, 1);
+ TEST_TRUE(batch, VA_Get_Size(slice) == 0, "exceed offset");
+ DECREF(slice);
+ }
+ {
+ VArray *wanted = VA_new(0);
+ VA_Push(wanted, (Obj*)CB_newf("9"));
+ VArray *slice = VA_Slice(array, 9, UINT32_MAX - 1);
+ TEST_TRUE(batch, VA_Get_Size(slice) == 1, "guard against overflow");
+ DECREF(slice);
+ DECREF(wanted);
+ }
+ DECREF(array);
+}
+
+static void
+test_Clone_and_Shallow_Copy(TestBatch *batch) {
+ VArray *array = VA_new(0);
+ VArray *twin;
+ uint32_t i;
+
+ for (i = 0; i < 10; i++) {
+ VA_Push(array, (Obj*)CB_newf("%u32", i));
+ }
+ twin = VA_Shallow_Copy(array);
+ TEST_TRUE(batch, VA_Equals(array, (Obj*)twin), "Shallow_Copy");
+ TEST_TRUE(batch, VA_Fetch(array, 1) == VA_Fetch(twin, 1),
+ "Shallow_Copy doesn't clone elements");
+ DECREF(twin);
+
+ twin = VA_Clone(array);
+ TEST_TRUE(batch, VA_Equals(array, (Obj*)twin), "Clone");
+ TEST_TRUE(batch, VA_Fetch(array, 1) != VA_Fetch(twin, 1),
+ "Clone performs deep clone");
+
+ DECREF(array);
+ DECREF(twin);
+}
+
+static void
+test_Dump_and_Load(TestBatch *batch) {
+ VArray *array = VA_new(0);
+ Obj *dump;
+ VArray *loaded;
+
+ VA_Push(array, (Obj*)S_new_cb("foo"));
+ dump = (Obj*)VA_Dump(array);
+ loaded = (VArray*)Obj_Load(dump, dump);
+ TEST_TRUE(batch, VA_Equals(array, (Obj*)loaded),
+ "Dump => Load round trip");
+
+ DECREF(array);
+ DECREF(dump);
+ DECREF(loaded);
+}
+
+static void
+test_serialization(TestBatch *batch) {
+ VArray *array = VA_new(0);
+ VArray *dupe;
+ VA_Store(array, 1, (Obj*)CB_newf("foo"));
+ VA_Store(array, 3, (Obj*)CB_newf("bar"));
+ dupe = (VArray*)TestUtils_freeze_thaw((Obj*)array);
+ TEST_TRUE(batch, dupe && VA_Equals(array, (Obj*)dupe),
+ "Round trip through FREEZE/THAW");
+ DECREF(dupe);
+ DECREF(array);
+}
+
+void
+TestVArray_run_tests(TestVArray *self) {
+ TestBatch *batch = (TestBatch*)self;
+ test_Equals(batch);
+ test_Store_Fetch(batch);
+ test_Push_Pop_Shift_Unshift(batch);
+ test_Delete(batch);
+ test_Resize(batch);
+ test_Excise(batch);
+ test_Push_VArray(batch);
+ test_Slice(batch);
+ test_Clone_and_Shallow_Copy(batch);
+ test_Dump_and_Load(batch);
+ test_serialization(batch);
+}
+
+
http://git-wip-us.apache.org/repos/asf/lucy/blob/9f826b31/core/Clownfish/Test/TestVArray.cfh
----------------------------------------------------------------------
diff --git a/core/Clownfish/Test/TestVArray.cfh b/core/Clownfish/Test/TestVArray.cfh
new file mode 100644
index 0000000..481dbb4
--- /dev/null
+++ b/core/Clownfish/Test/TestVArray.cfh
@@ -0,0 +1,32 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+parcel TestClownfish;
+
+class Clownfish::Test::TestVArray
+ inherits Clownfish::Test::TestBatch {
+
+ inert incremented TestVArray*
+ new(TestFormatter *formatter);
+
+ inert TestVArray*
+ init(TestVArray *self, TestFormatter *formatter);
+
+ void
+ Run_Tests(TestVArray *self);
+}
+
+
http://git-wip-us.apache.org/repos/asf/lucy/blob/9f826b31/core/Clownfish/Test/Util/TestAtomic.c
----------------------------------------------------------------------
diff --git a/core/Clownfish/Test/Util/TestAtomic.c b/core/Clownfish/Test/Util/TestAtomic.c
new file mode 100644
index 0000000..3610f7d
--- /dev/null
+++ b/core/Clownfish/Test/Util/TestAtomic.c
@@ -0,0 +1,71 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define CFISH_USE_SHORT_NAMES
+#define TESTCFISH_USE_SHORT_NAMES
+
+#include "Clownfish/Test/Util/TestAtomic.h"
+
+#include "Clownfish/Test.h"
+#include "Clownfish/Test/TestFormatter.h"
+#include "Clownfish/Util/Atomic.h"
+#include "Clownfish/VTable.h"
+
+TestAtomic*
+TestAtomic_new(TestFormatter *formatter) {
+ TestAtomic *self = (TestAtomic*)VTable_Make_Obj(TESTATOMIC);
+ return TestAtomic_init(self, formatter);
+}
+
+TestAtomic*
+TestAtomic_init(TestAtomic *self, TestFormatter *formatter) {
+ return (TestAtomic*)TestBatch_init((TestBatch*)self, 6, formatter);
+}
+
+static void
+test_cas_ptr(TestBatch *batch) {
+ int foo = 1;
+ int bar = 2;
+ int *foo_pointer = &foo;
+ int *bar_pointer = &bar;
+ int *target = NULL;
+
+ TEST_TRUE(batch,
+ Atomic_cas_ptr((void**)&target, NULL, foo_pointer),
+ "cas_ptr returns true on success");
+ TEST_TRUE(batch, target == foo_pointer, "cas_ptr sets target");
+
+ target = NULL;
+ TEST_FALSE(batch,
+ Atomic_cas_ptr((void**)&target, bar_pointer, foo_pointer),
+ "cas_ptr returns false when it old_value doesn't match");
+ TEST_TRUE(batch, target == NULL,
+ "cas_ptr doesn't do anything to target when old_value doesn't match");
+
+ target = foo_pointer;
+ TEST_TRUE(batch,
+ Atomic_cas_ptr((void**)&target, foo_pointer, bar_pointer),
+ "cas_ptr from one value to another");
+ TEST_TRUE(batch, target == bar_pointer, "cas_ptr sets target");
+}
+
+void
+TestAtomic_run_tests(TestAtomic *self) {
+ TestBatch *batch = (TestBatch*)self;
+ test_cas_ptr(batch);
+}
+
+
http://git-wip-us.apache.org/repos/asf/lucy/blob/9f826b31/core/Clownfish/Test/Util/TestAtomic.cfh
----------------------------------------------------------------------
diff --git a/core/Clownfish/Test/Util/TestAtomic.cfh b/core/Clownfish/Test/Util/TestAtomic.cfh
new file mode 100644
index 0000000..90d905e
--- /dev/null
+++ b/core/Clownfish/Test/Util/TestAtomic.cfh
@@ -0,0 +1,32 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+parcel TestClownfish;
+
+class Clownfish::Test::Util::TestAtomic
+ inherits Clownfish::Test::TestBatch {
+
+ inert incremented TestAtomic*
+ new(TestFormatter *formatter);
+
+ inert TestAtomic*
+ init(TestAtomic *self, TestFormatter *formatter);
+
+ void
+ Run_Tests(TestAtomic *self);
+}
+
+
http://git-wip-us.apache.org/repos/asf/lucy/blob/9f826b31/core/Clownfish/Test/Util/TestMemory.c
----------------------------------------------------------------------
diff --git a/core/Clownfish/Test/Util/TestMemory.c b/core/Clownfish/Test/Util/TestMemory.c
new file mode 100644
index 0000000..a3ac538
--- /dev/null
+++ b/core/Clownfish/Test/Util/TestMemory.c
@@ -0,0 +1,124 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define CHY_USE_SHORT_NAMES
+#define CFISH_USE_SHORT_NAMES
+#define TESTCFISH_USE_SHORT_NAMES
+
+#include "Clownfish/Test/Util/TestMemory.h"
+
+#include "Clownfish/Test.h"
+#include "Clownfish/Test/TestFormatter.h"
+#include "Clownfish/Util/Memory.h"
+#include "Clownfish/VTable.h"
+
+TestMemory*
+TestMemory_new(TestFormatter *formatter) {
+ TestMemory *self = (TestMemory*)VTable_Make_Obj(TESTMEMORY);
+ return TestMemory_init(self, formatter);
+}
+
+TestMemory*
+TestMemory_init(TestMemory *self, TestFormatter *formatter) {
+ return (TestMemory*)TestBatch_init((TestBatch*)self, 30, formatter);
+}
+
+static void
+test_oversize__growth_rate(TestBatch *batch) {
+ bool success = true;
+ uint64_t size = 0;
+ double growth_count = 0;
+ double average_growth_rate = 0.0;
+
+ while (size < SIZE_MAX) {
+ uint64_t next_size = Memory_oversize((size_t)size + 1, sizeof(void*));
+ if (next_size < size) {
+ success = false;
+ FAIL(batch, "Asked for %" PRId64 ", got smaller amount %" PRId64,
+ size + 1, next_size);
+ break;
+ }
+ if (size > 0) {
+ growth_count += 1;
+ double growth_rate = U64_TO_DOUBLE(next_size) /
+ U64_TO_DOUBLE(size);
+ double sum = growth_rate + (growth_count - 1) * average_growth_rate;
+ average_growth_rate = sum / growth_count;
+ if (average_growth_rate < 1.1) {
+ FAIL(batch, "Average growth rate dropped below 1.1x: %f",
+ average_growth_rate);
+ success = false;
+ break;
+ }
+ }
+ size = next_size;
+ }
+ TEST_TRUE(batch, growth_count > 0, "Grew %f times", growth_count);
+ if (success) {
+ TEST_TRUE(batch, average_growth_rate > 1.1,
+ "Growth rate of oversize() averages above 1.1: %.3f",
+ average_growth_rate);
+ }
+
+ for (int minimum = 1; minimum < 8; minimum++) {
+ uint64_t next_size = Memory_oversize(minimum, sizeof(void*));
+ double growth_rate = U64_TO_DOUBLE(next_size) / (double)minimum;
+ TEST_TRUE(batch, growth_rate > 1.2,
+ "Growth rate is higher for smaller arrays (%d, %.3f)", minimum,
+ growth_rate);
+ }
+}
+
+static void
+test_oversize__ceiling(TestBatch *batch) {
+ for (int width = 0; width < 10; width++) {
+ size_t size = Memory_oversize(SIZE_MAX, width);
+ TEST_TRUE(batch, size == SIZE_MAX,
+ "Memory_oversize hits ceiling at SIZE_MAX (width %d)", width);
+ size = Memory_oversize(SIZE_MAX - 1, width);
+ TEST_TRUE(batch, size == SIZE_MAX,
+ "Memory_oversize hits ceiling at SIZE_MAX (width %d)", width);
+ }
+}
+
+static void
+test_oversize__rounding(TestBatch *batch) {
+ int widths[] = { 1, 2, 4, 0 };
+
+ for (int width_tick = 0; widths[width_tick] != 0; width_tick++) {
+ int width = widths[width_tick];
+ for (int i = 0; i < 25; i++) {
+ size_t size = Memory_oversize(i, width);
+ size_t bytes = size * width;
+ if (bytes % sizeof(void*) != 0) {
+ FAIL(batch, "Rounding failure for %d, width %d",
+ i, width);
+ return;
+ }
+ }
+ }
+ PASS(batch, "Round allocations up to the size of a pointer");
+}
+
+void
+TestMemory_run_tests(TestMemory *self) {
+ TestBatch *batch = (TestBatch*)self;
+ test_oversize__growth_rate(batch);
+ test_oversize__ceiling(batch);
+ test_oversize__rounding(batch);
+}
+
+
http://git-wip-us.apache.org/repos/asf/lucy/blob/9f826b31/core/Clownfish/Test/Util/TestMemory.cfh
----------------------------------------------------------------------
diff --git a/core/Clownfish/Test/Util/TestMemory.cfh b/core/Clownfish/Test/Util/TestMemory.cfh
new file mode 100644
index 0000000..23252c2
--- /dev/null
+++ b/core/Clownfish/Test/Util/TestMemory.cfh
@@ -0,0 +1,32 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+parcel TestClownfish;
+
+class Clownfish::Test::Util::TestMemory
+ inherits Clownfish::Test::TestBatch {
+
+ inert incremented TestMemory*
+ new(TestFormatter *formatter);
+
+ inert TestMemory*
+ init(TestMemory *self, TestFormatter *formatter);
+
+ void
+ Run_Tests(TestMemory *self);
+}
+
+
http://git-wip-us.apache.org/repos/asf/lucy/blob/9f826b31/core/Clownfish/Test/Util/TestNumberUtils.c
----------------------------------------------------------------------
diff --git a/core/Clownfish/Test/Util/TestNumberUtils.c b/core/Clownfish/Test/Util/TestNumberUtils.c
new file mode 100644
index 0000000..b524818
--- /dev/null
+++ b/core/Clownfish/Test/Util/TestNumberUtils.c
@@ -0,0 +1,381 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+#include <time.h>
+
+#define CFISH_USE_SHORT_NAMES
+#define TESTCFISH_USE_SHORT_NAMES
+
+#include "Clownfish/Test/Util/TestNumberUtils.h"
+
+#include "Clownfish/Err.h"
+#include "Clownfish/Test.h"
+#include "Clownfish/Test/TestFormatter.h"
+#include "Clownfish/Test/TestUtils.h"
+#include "Clownfish/Util/Memory.h"
+#include "Clownfish/Util/NumberUtils.h"
+#include "Clownfish/VTable.h"
+
+TestNumberUtils*
+TestNumUtil_new(TestFormatter *formatter) {
+ TestNumberUtils *self = (TestNumberUtils*)VTable_Make_Obj(TESTNUMBERUTILS);
+ return TestNumUtil_init(self, formatter);
+}
+
+TestNumberUtils*
+TestNumUtil_init(TestNumberUtils *self, TestFormatter *formatter) {
+ return (TestNumberUtils*)TestBatch_init((TestBatch*)self, 1196, formatter);
+}
+
+static void
+test_u1(TestBatch *batch) {
+ size_t count = 64;
+ uint64_t *ints = TestUtils_random_u64s(NULL, count, 0, 2);
+ size_t amount = count / 8;
+ uint8_t *bits = (uint8_t*)CALLOCATE(amount, sizeof(uint8_t));
+
+ for (size_t i = 0; i < count; i++) {
+ if (ints[i]) { NumUtil_u1set(bits, i); }
+ }
+ for (size_t i = 0; i < count; i++) {
+ TEST_INT_EQ(batch, NumUtil_u1get(bits, i), (long)ints[i],
+ "u1 set/get");
+ }
+
+ for (size_t i = 0; i < count; i++) {
+ NumUtil_u1flip(bits, i);
+ }
+ for (size_t i = 0; i < count; i++) {
+ TEST_INT_EQ(batch, NumUtil_u1get(bits, i), !ints[i], "u1 flip");
+ }
+
+ FREEMEM(bits);
+ FREEMEM(ints);
+}
+
+static void
+test_u2(TestBatch *batch) {
+ size_t count = 32;
+ uint64_t *ints = TestUtils_random_u64s(NULL, count, 0, 4);
+ uint8_t *bits = (uint8_t*)CALLOCATE((count / 4), sizeof(uint8_t));
+
+ for (size_t i = 0; i < count; i++) {
+ NumUtil_u2set(bits, i, (uint8_t)ints[i]);
+ }
+ for (size_t i = 0; i < count; i++) {
+ TEST_INT_EQ(batch, NumUtil_u2get(bits, i), (long)ints[i], "u2");
+ }
+
+ FREEMEM(bits);
+ FREEMEM(ints);
+}
+
+static void
+test_u4(TestBatch *batch) {
+ size_t count = 128;
+ uint64_t *ints = TestUtils_random_u64s(NULL, count, 0, 16);
+ uint8_t *bits = (uint8_t*)CALLOCATE((count / 2), sizeof(uint8_t));
+
+ for (size_t i = 0; i < count; i++) {
+ NumUtil_u4set(bits, i, (uint8_t)ints[i]);
+ }
+ for (size_t i = 0; i < count; i++) {
+ TEST_INT_EQ(batch, NumUtil_u4get(bits, i), (long)ints[i], "u4");
+ }
+
+ FREEMEM(bits);
+ FREEMEM(ints);
+}
+
+static void
+test_c32(TestBatch *batch) {
+ uint64_t mins[] = { 0, 0x4000 - 100, (uint32_t)INT32_MAX - 100, UINT32_MAX - 10 };
+ uint64_t limits[] = { 500, 0x4000 + 100, (uint32_t)INT32_MAX + 100, UINT32_MAX };
+ uint32_t set_num;
+ uint32_t num_sets = sizeof(mins) / sizeof(uint64_t);
+ size_t count = 64;
+ uint64_t *ints = NULL;
+ size_t amount = count * C32_MAX_BYTES;
+ char *encoded = (char*)CALLOCATE(amount, sizeof(char));
+ char *target = encoded;
+ char *limit = target + amount;
+
+ for (set_num = 0; set_num < num_sets; set_num++) {
+ char *skip;
+ ints = TestUtils_random_u64s(ints, count,
+ mins[set_num], limits[set_num]);
+ target = encoded;
+ for (size_t i = 0; i < count; i++) {
+ NumUtil_encode_c32((uint32_t)ints[i], &target);
+ }
+ target = encoded;
+ skip = encoded;
+ for (size_t i = 0; i < count; i++) {
+ TEST_INT_EQ(batch, NumUtil_decode_c32(&target), (long)ints[i],
+ "c32 %lu", (long)ints[i]);
+ NumUtil_skip_cint(&skip);
+ if (target > limit) { THROW(ERR, "overrun"); }
+ }
+ TEST_TRUE(batch, skip == target, "skip %lu == %lu",
+ (unsigned long)skip, (unsigned long)target);
+
+ target = encoded;
+ for (size_t i = 0; i < count; i++) {
+ NumUtil_encode_padded_c32((uint32_t)ints[i], &target);
+ }
+ TEST_TRUE(batch, target == limit,
+ "padded c32 uses 5 bytes (%lu == %lu)", (unsigned long)target,
+ (unsigned long)limit);
+ target = encoded;
+ skip = encoded;
+ for (size_t i = 0; i < count; i++) {
+ TEST_INT_EQ(batch, NumUtil_decode_c32(&target), (long)ints[i],
+ "padded c32 %lu", (long)ints[i]);
+ NumUtil_skip_cint(&skip);
+ if (target > limit) { THROW(ERR, "overrun"); }
+ }
+ TEST_TRUE(batch, skip == target, "skip padded %lu == %lu",
+ (unsigned long)skip, (unsigned long)target);
+ }
+
+ target = encoded;
+ NumUtil_encode_c32(UINT32_MAX, &target);
+ target = encoded;
+ TEST_INT_EQ(batch, NumUtil_decode_c32(&target), UINT32_MAX, "c32 UINT32_MAX");
+
+ FREEMEM(encoded);
+ FREEMEM(ints);
+}
+
+static void
+test_c64(TestBatch *batch) {
+ uint64_t mins[] = { 0, 0x4000 - 100, (uint64_t)UINT32_MAX - 100, UINT64_MAX - 10 };
+ uint64_t limits[] = { 500, 0x4000 + 100, (uint64_t)UINT32_MAX + 1000, UINT64_MAX };
+ uint32_t set_num;
+ uint32_t num_sets = sizeof(mins) / sizeof(uint64_t);
+ size_t count = 64;
+ uint64_t *ints = NULL;
+ size_t amount = count * C64_MAX_BYTES;
+ char *encoded = (char*)CALLOCATE(amount, sizeof(char));
+ char *target = encoded;
+ char *limit = target + amount;
+
+ for (set_num = 0; set_num < num_sets; set_num++) {
+ char *skip;
+ ints = TestUtils_random_u64s(ints, count,
+ mins[set_num], limits[set_num]);
+ target = encoded;
+ for (size_t i = 0; i < count; i++) {
+ NumUtil_encode_c64(ints[i], &target);
+ }
+ target = encoded;
+ skip = encoded;
+ for (size_t i = 0; i < count; i++) {
+ uint64_t got = NumUtil_decode_c64(&target);
+ TEST_TRUE(batch, got == ints[i],
+ "c64 %" PRIu64 " == %" PRIu64, got, ints[i]);
+ if (target > limit) { THROW(ERR, "overrun"); }
+ NumUtil_skip_cint(&skip);
+ }
+ TEST_TRUE(batch, skip == target, "skip %lu == %lu",
+ (unsigned long)skip, (unsigned long)target);
+ }
+
+ target = encoded;
+ NumUtil_encode_c64(UINT64_MAX, &target);
+ target = encoded;
+
+ uint64_t got = NumUtil_decode_c64(&target);
+ TEST_TRUE(batch, got == UINT64_MAX, "c64 UINT64_MAX");
+
+ FREEMEM(encoded);
+ FREEMEM(ints);
+}
+
+static void
+test_bigend_u16(TestBatch *batch) {
+ size_t count = 32;
+ uint64_t *ints = TestUtils_random_u64s(NULL, count, 0, UINT16_MAX + 1);
+ size_t amount = (count + 1) * sizeof(uint16_t);
+ char *allocated = (char*)CALLOCATE(amount, sizeof(char));
+ char *encoded = allocated + 1; // Intentionally misaligned.
+ char *target = encoded;
+
+ for (size_t i = 0; i < count; i++) {
+ NumUtil_encode_bigend_u16((uint16_t)ints[i], &target);
+ target += sizeof(uint16_t);
+ }
+ target = encoded;
+ for (size_t i = 0; i < count; i++) {
+ uint16_t got = NumUtil_decode_bigend_u16(target);
+ TEST_INT_EQ(batch, got, (long)ints[i], "bigend u16");
+ target += sizeof(uint16_t);
+ }
+
+ target = encoded;
+ NumUtil_encode_bigend_u16(1, &target);
+ TEST_INT_EQ(batch, encoded[0], 0, "Truly big-endian u16");
+ TEST_INT_EQ(batch, encoded[1], 1, "Truly big-endian u16");
+
+ FREEMEM(allocated);
+ FREEMEM(ints);
+}
+
+static void
+test_bigend_u32(TestBatch *batch) {
+ size_t count = 32;
+ uint64_t *ints = TestUtils_random_u64s(NULL, count, 0, UINT64_C(1) + UINT32_MAX);
+ size_t amount = (count + 1) * sizeof(uint32_t);
+ char *allocated = (char*)CALLOCATE(amount, sizeof(char));
+ char *encoded = allocated + 1; // Intentionally misaligned.
+ char *target = encoded;
+
+ for (size_t i = 0; i < count; i++) {
+ NumUtil_encode_bigend_u32((uint32_t)ints[i], &target);
+ target += sizeof(uint32_t);
+ }
+ target = encoded;
+ for (size_t i = 0; i < count; i++) {
+ uint32_t got = NumUtil_decode_bigend_u32(target);
+ TEST_INT_EQ(batch, got, (long)ints[i], "bigend u32");
+ target += sizeof(uint32_t);
+ }
+
+ target = encoded;
+ NumUtil_encode_bigend_u32(1, &target);
+ TEST_INT_EQ(batch, encoded[0], 0, "Truly big-endian u32");
+ TEST_INT_EQ(batch, encoded[3], 1, "Truly big-endian u32");
+
+ FREEMEM(allocated);
+ FREEMEM(ints);
+}
+
+static void
+test_bigend_u64(TestBatch *batch) {
+ size_t count = 32;
+ uint64_t *ints = TestUtils_random_u64s(NULL, count, 0, UINT64_MAX);
+ size_t amount = (count + 1) * sizeof(uint64_t);
+ char *allocated = (char*)CALLOCATE(amount, sizeof(char));
+ char *encoded = allocated + 1; // Intentionally misaligned.
+ char *target = encoded;
+
+ for (size_t i = 0; i < count; i++) {
+ NumUtil_encode_bigend_u64(ints[i], &target);
+ target += sizeof(uint64_t);
+ }
+ target = encoded;
+ for (size_t i = 0; i < count; i++) {
+ uint64_t got = NumUtil_decode_bigend_u64(target);
+ TEST_TRUE(batch, got == ints[i], "bigend u64");
+ target += sizeof(uint64_t);
+ }
+
+ target = encoded;
+ NumUtil_encode_bigend_u64(1, &target);
+ TEST_INT_EQ(batch, encoded[0], 0, "Truly big-endian");
+ TEST_INT_EQ(batch, encoded[7], 1, "Truly big-endian");
+
+ FREEMEM(allocated);
+ FREEMEM(ints);
+}
+
+static void
+test_bigend_f32(TestBatch *batch) {
+ float source[] = { -1.3f, 0.0f, 100.2f };
+ size_t count = 3;
+ size_t amount = (count + 1) * sizeof(float);
+ uint8_t *allocated = (uint8_t*)CALLOCATE(amount, sizeof(uint8_t));
+ uint8_t *encoded = allocated + 1; // Intentionally misaligned.
+ uint8_t *target = encoded;
+
+ for (size_t i = 0; i < count; i++) {
+ NumUtil_encode_bigend_f32(source[i], &target);
+ target += sizeof(float);
+ }
+ target = encoded;
+ for (size_t i = 0; i < count; i++) {
+ float got = NumUtil_decode_bigend_f32(target);
+ TEST_TRUE(batch, got == source[i], "bigend f32");
+ target += sizeof(float);
+ }
+
+ target = encoded;
+ NumUtil_encode_bigend_f32(-2.0f, &target);
+ TEST_INT_EQ(batch, (encoded[0] & 0x80), 0x80,
+ "Truly big-endian (IEEE 754 sign bit set for negative number)");
+ TEST_INT_EQ(batch, encoded[0], 0xC0,
+ "IEEE 754 representation of -2.0f, byte 0");
+ for (size_t i = 1; i < sizeof(float); i++) {
+ TEST_INT_EQ(batch, encoded[i], 0,
+ "IEEE 754 representation of -2.0f, byte %d", (int)i);
+ }
+
+ FREEMEM(allocated);
+}
+
+static void
+test_bigend_f64(TestBatch *batch) {
+ double source[] = { -1.3, 0.0, 100.2 };
+ size_t count = 3;
+ size_t amount = (count + 1) * sizeof(double);
+ uint8_t *allocated = (uint8_t*)CALLOCATE(amount, sizeof(uint8_t));
+ uint8_t *encoded = allocated + 1; // Intentionally misaligned.
+ uint8_t *target = encoded;
+
+ for (size_t i = 0; i < count; i++) {
+ NumUtil_encode_bigend_f64(source[i], &target);
+ target += sizeof(double);
+ }
+ target = encoded;
+ for (size_t i = 0; i < count; i++) {
+ double got = NumUtil_decode_bigend_f64(target);
+ TEST_TRUE(batch, got == source[i], "bigend f64");
+ target += sizeof(double);
+ }
+
+ target = encoded;
+ NumUtil_encode_bigend_f64(-2.0, &target);
+ TEST_INT_EQ(batch, (encoded[0] & 0x80), 0x80,
+ "Truly big-endian (IEEE 754 sign bit set for negative number)");
+ TEST_INT_EQ(batch, encoded[0], 0xC0,
+ "IEEE 754 representation of -2.0, byte 0");
+ for (size_t i = 1; i < sizeof(double); i++) {
+ TEST_INT_EQ(batch, encoded[i], 0,
+ "IEEE 754 representation of -2.0, byte %d", (int)i);
+ }
+
+ FREEMEM(allocated);
+}
+
+void
+TestNumUtil_run_tests(TestNumberUtils *self) {
+ TestBatch *batch = (TestBatch*)self;
+ srand((unsigned int)time((time_t*)NULL));
+ test_u1(batch);
+ test_u2(batch);
+ test_u4(batch);
+ test_c32(batch);
+ test_c64(batch);
+ test_bigend_u16(batch);
+ test_bigend_u32(batch);
+ test_bigend_u64(batch);
+ test_bigend_f32(batch);
+ test_bigend_f64(batch);
+}
+
+
+
http://git-wip-us.apache.org/repos/asf/lucy/blob/9f826b31/core/Clownfish/Test/Util/TestNumberUtils.cfh
----------------------------------------------------------------------
diff --git a/core/Clownfish/Test/Util/TestNumberUtils.cfh b/core/Clownfish/Test/Util/TestNumberUtils.cfh
new file mode 100644
index 0000000..1c6659e
--- /dev/null
+++ b/core/Clownfish/Test/Util/TestNumberUtils.cfh
@@ -0,0 +1,32 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+parcel TestClownfish;
+
+class Clownfish::Test::Util::TestNumberUtils cnick TestNumUtil
+ inherits Clownfish::Test::TestBatch {
+
+ inert incremented TestNumberUtils*
+ new(TestFormatter *formatter);
+
+ inert TestNumberUtils*
+ init(TestNumberUtils *self, TestFormatter *formatter);
+
+ void
+ Run_Tests(TestNumberUtils *self);
+}
+
+
http://git-wip-us.apache.org/repos/asf/lucy/blob/9f826b31/core/Clownfish/Test/Util/TestStringHelper.c
----------------------------------------------------------------------
diff --git a/core/Clownfish/Test/Util/TestStringHelper.c b/core/Clownfish/Test/Util/TestStringHelper.c
new file mode 100644
index 0000000..18cc4db
--- /dev/null
+++ b/core/Clownfish/Test/Util/TestStringHelper.c
@@ -0,0 +1,361 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string.h>
+
+#define CFISH_USE_SHORT_NAMES
+#define TESTCFISH_USE_SHORT_NAMES
+
+#include "Clownfish/Test/Util/TestStringHelper.h"
+
+#include "Clownfish/CharBuf.h"
+#include "Clownfish/Err.h"
+#include "Clownfish/Test.h"
+#include "Clownfish/Test/TestFormatter.h"
+#include "Clownfish/Test/TestUtils.h"
+#include "Clownfish/Util/StringHelper.h"
+#include "Clownfish/VTable.h"
+#include "Lucy/Util/Json.h"
+#include "utf8proc.h"
+
+/* This alternative implementation of utf8_valid() is (presumably) slower, but
+ * it implements the standard in a more linear, easy-to-grok way.
+ */
+#define TRAIL_OK(n) (n >= 0x80 && n <= 0xBF)
+TestStringHelper*
+TestStrHelp_new(TestFormatter *formatter) {
+ TestStringHelper *self = (TestStringHelper*)VTable_Make_Obj(TESTSTRINGHELPER);
+ return TestStrHelp_init(self, formatter);
+}
+
+TestStringHelper*
+TestStrHelp_init(TestStringHelper *self, TestFormatter *formatter) {
+ return (TestStringHelper*)TestBatch_init((TestBatch*)self, 41, formatter);
+}
+
+static bool
+S_utf8_valid_alt(const char *maybe_utf8, size_t size) {
+ const uint8_t *string = (const uint8_t*)maybe_utf8;
+ const uint8_t *const end = string + size;
+ while (string < end) {
+ int count = StrHelp_UTF8_COUNT[*string];
+ bool valid = false;
+ if (count == 1) {
+ if (string[0] <= 0x7F) {
+ valid = true;
+ }
+ }
+ else if (count == 2) {
+ if (string[0] >= 0xC2 && string[0] <= 0xDF) {
+ if (TRAIL_OK(string[1])) {
+ valid = true;
+ }
+ }
+ }
+ else if (count == 3) {
+ if (string[0] == 0xE0) {
+ if (string[1] >= 0xA0 && string[1] <= 0xBF
+ && TRAIL_OK(string[2])
+ ) {
+ valid = true;
+ }
+ }
+ else if (string[0] >= 0xE1 && string[0] <= 0xEC) {
+ if (TRAIL_OK(string[1])
+ && TRAIL_OK(string[2])
+ ) {
+ valid = true;
+ }
+ }
+ else if (string[0] == 0xED) {
+ if (string[1] >= 0x80 && string[1] <= 0x9F
+ && TRAIL_OK(string[2])
+ ) {
+ valid = true;
+ }
+ }
+ else if (string[0] >= 0xEE && string[0] <= 0xEF) {
+ if (TRAIL_OK(string[1])
+ && TRAIL_OK(string[2])
+ ) {
+ valid = true;
+ }
+ }
+ }
+ else if (count == 4) {
+ if (string[0] == 0xF0) {
+ if (string[1] >= 0x90 && string[1] <= 0xBF
+ && TRAIL_OK(string[2])
+ && TRAIL_OK(string[3])
+ ) {
+ valid = true;
+ }
+ }
+ else if (string[0] >= 0xF1 && string[0] <= 0xF3) {
+ if (TRAIL_OK(string[1])
+ && TRAIL_OK(string[2])
+ && TRAIL_OK(string[3])
+ ) {
+ valid = true;
+ }
+ }
+ else if (string[0] == 0xF4) {
+ if (string[1] >= 0x80 && string[1] <= 0x8F
+ && TRAIL_OK(string[2])
+ && TRAIL_OK(string[3])
+ ) {
+ valid = true;
+ }
+ }
+ }
+
+ if (!valid) {
+ return false;
+ }
+ string += count;
+ }
+
+ if (string != end) {
+ return false;
+ }
+
+ return true;
+}
+
+static void
+test_overlap(TestBatch *batch) {
+ int32_t result;
+ result = StrHelp_overlap("", "", 0, 0);
+ TEST_INT_EQ(batch, result, 0, "two empty strings");
+ result = StrHelp_overlap("", "foo", 0, 3);
+ TEST_INT_EQ(batch, result, 0, "first string is empty");
+ result = StrHelp_overlap("foo", "", 3, 0);
+ TEST_INT_EQ(batch, result, 0, "second string is empty");
+ result = StrHelp_overlap("foo", "foo", 3, 3);
+ TEST_INT_EQ(batch, result, 3, "equal strings");
+ result = StrHelp_overlap("foo bar", "foo", 7, 3);
+ TEST_INT_EQ(batch, result, 3, "first string is longer");
+ result = StrHelp_overlap("foo", "foo bar", 3, 7);
+ TEST_INT_EQ(batch, result, 3, "second string is longer");
+}
+
+
+static void
+test_to_base36(TestBatch *batch) {
+ char buffer[StrHelp_MAX_BASE36_BYTES];
+ StrHelp_to_base36(UINT64_MAX, buffer);
+ TEST_STR_EQ(batch, "3w5e11264sgsf", buffer, "base36 UINT64_MAX");
+ StrHelp_to_base36(1, buffer);
+ TEST_STR_EQ(batch, "1", buffer, "base36 1");
+ TEST_INT_EQ(batch, buffer[1], 0, "base36 NULL termination");
+}
+
+static void
+test_utf8_round_trip(TestBatch *batch) {
+ uint32_t code_point;
+ for (code_point = 0; code_point <= 0x10FFFF; code_point++) {
+ char buffer[4];
+ uint32_t size = StrHelp_encode_utf8_char(code_point, buffer);
+ char *start = buffer;
+ char *end = start + size;
+
+ // Verify length returned by encode_utf8_char().
+ if (size != StrHelp_UTF8_COUNT[(unsigned char)buffer[0]]) {
+ break;
+ }
+ // Verify that utf8_valid() agrees with alternate implementation.
+ if (!!StrHelp_utf8_valid(start, size)
+ != !!S_utf8_valid_alt(start, size)
+ ) {
+ break;
+ }
+
+ // Verify back_utf8_char().
+ if (StrHelp_back_utf8_char(end, start) != start) {
+ break;
+ }
+
+ // Verify round trip of encode/decode.
+ if (StrHelp_decode_utf8_char(buffer) != code_point) {
+ break;
+ }
+ }
+ if (code_point == 0x110000) {
+ PASS(batch, "Successfully round tripped 0 - 0x10FFFF");
+ }
+ else {
+ FAIL(batch, "Failed round trip at 0x%.1X", (unsigned)code_point);
+ }
+}
+
+static void
+S_test_validity(TestBatch *batch, const char *content, size_t size,
+ bool expected, const char *description) {
+ bool sane = StrHelp_utf8_valid(content, size);
+ bool double_check = S_utf8_valid_alt(content, size);
+ if (sane != double_check) {
+ FAIL(batch, "Disagreement: %s", description);
+ }
+ else {
+ TEST_TRUE(batch, sane == expected, "%s", description);
+ }
+}
+
+static void
+test_utf8_valid(TestBatch *batch) {
+ // Musical symbol G clef:
+ // Code point: U+1D11E
+ // UTF-16: 0xD834 0xDD1E
+ // UTF-8 0xF0 0x9D 0x84 0x9E
+ S_test_validity(batch, "\xF0\x9D\x84\x9E", 4, true,
+ "Musical symbol G clef");
+ S_test_validity(batch, "\xED\xA0\xB4\xED\xB4\x9E", 6, false,
+ "G clef as UTF-8 encoded UTF-16 surrogates");
+ S_test_validity(batch, ".\xED\xA0\xB4.", 5, false,
+ "Isolated high surrogate");
+ S_test_validity(batch, ".\xED\xB4\x9E.", 5, false,
+ "Isolated low surrogate");
+
+ // Shortest form.
+ S_test_validity(batch, ".\xC1\x9C.", 4, false,
+ "Non-shortest form ASCII backslash");
+ S_test_validity(batch, ".\xC0\xAF.", 4, false,
+ "Non-shortest form ASCII slash");
+ S_test_validity(batch, ".\xC0\x80.", 4, false,
+ "Non-shortest form ASCII NUL character");
+
+ // Range.
+ S_test_validity(batch, "\xF8\x88\x80\x80\x80", 5, false, "5-byte UTF-8");
+
+ // Bad continuations.
+ S_test_validity(batch, "\xE2\x98\xBA\xE2\x98\xBA", 6, true,
+ "SmileySmiley");
+ S_test_validity(batch, "\xE2\xBA\xE2\x98\xBA", 5, false,
+ "missing first continuation byte");
+ S_test_validity(batch, "\xE2\x98\xE2\x98\xBA", 5, false,
+ "missing second continuation byte");
+ S_test_validity(batch, "\xE2\xE2\x98\xBA", 4, false,
+ "missing both continuation bytes");
+ S_test_validity(batch, "\xBA\xE2\x98\xBA\xE2\xBA", 5, false,
+ "missing first continuation byte (end)");
+ S_test_validity(batch, "\xE2\x98\xBA\xE2\x98", 5, false,
+ "missing second continuation byte (end)");
+ S_test_validity(batch, "\xE2\x98\xBA\xE2", 4, false,
+ "missing both continuation bytes (end)");
+ S_test_validity(batch, "\xBA\xE2\x98\xBA", 4, false,
+ "isolated continuation byte 0xBA");
+ S_test_validity(batch, "\x98\xE2\x98\xBA", 4, false,
+ "isolated continuation byte 0x98");
+ S_test_validity(batch, "\xE2\x98\xBA\xBA", 4, false,
+ "isolated continuation byte 0xBA (end)");
+ S_test_validity(batch, "\xE2\x98\xBA\x98", 4, false,
+ "isolated continuation byte 0x98 (end)");
+}
+
+static void
+test_is_whitespace(TestBatch *batch) {
+ TEST_TRUE(batch, StrHelp_is_whitespace(' '), "space is whitespace");
+ TEST_TRUE(batch, StrHelp_is_whitespace('\n'), "newline is whitespace");
+ TEST_TRUE(batch, StrHelp_is_whitespace('\t'), "tab is whitespace");
+ TEST_TRUE(batch, StrHelp_is_whitespace('\v'),
+ "vertical tab is whitespace");
+ TEST_TRUE(batch, StrHelp_is_whitespace(0x180E),
+ "Mongolian vowel separator is whitespace");
+ TEST_FALSE(batch, StrHelp_is_whitespace('a'), "'a' isn't whitespace");
+ TEST_FALSE(batch, StrHelp_is_whitespace(0), "NULL isn't whitespace");
+ TEST_FALSE(batch, StrHelp_is_whitespace(0x263A),
+ "Smiley isn't whitespace");
+}
+
+static void
+test_back_utf8_char(TestBatch *batch) {
+ char buffer[4];
+ char *buf = buffer + 1;
+ uint32_t len = StrHelp_encode_utf8_char(0x263A, buffer);
+ char *end = buffer + len;
+ TEST_TRUE(batch, StrHelp_back_utf8_char(end, buffer) == buffer,
+ "back_utf8_char");
+ TEST_TRUE(batch, StrHelp_back_utf8_char(end, buf) == NULL,
+ "back_utf8_char returns NULL rather than back up beyond start");
+ TEST_TRUE(batch, StrHelp_back_utf8_char(buffer, buffer) == NULL,
+ "back_utf8_char returns NULL when end == start");
+}
+
+static void
+test_utf8proc_normalization(TestBatch *batch) {
+ SKIP(batch, "utf8proc can't handle control chars or Unicode non-chars");
+ return;
+
+ for (int32_t i = 0; i < 100; i++) {
+ CharBuf *source = TestUtils_random_string(rand() % 40);
+
+ // Normalize once.
+ uint8_t *normalized;
+ int32_t check = utf8proc_map(CB_Get_Ptr8(source), CB_Get_Size(source),
+ &normalized,
+ UTF8PROC_STABLE |
+ UTF8PROC_COMPOSE |
+ UTF8PROC_COMPAT |
+ UTF8PROC_CASEFOLD);
+ if (check < 0) {
+ lucy_Json_set_tolerant(1);
+ CharBuf *json = lucy_Json_to_json((Obj*)source);
+ if (!json) {
+ json = CB_newf("[failed to encode]");
+ }
+ FAIL(batch, "Failed to normalize: %s", CB_Get_Ptr8(json));
+ DECREF(json);
+ DECREF(source);
+ return;
+ }
+
+ // Normalize again.
+ size_t normalized_len = strlen((char*)normalized);
+ uint8_t *dupe;
+ int32_t dupe_check = utf8proc_map(normalized, normalized_len, &dupe,
+ UTF8PROC_STABLE |
+ UTF8PROC_COMPOSE |
+ UTF8PROC_COMPAT |
+ UTF8PROC_CASEFOLD);
+ if (dupe_check < 0) {
+ THROW(ERR, "Unexpected normalization error: %i32", dupe_check);
+ }
+ int comparison = strcmp((char*)normalized, (char*)dupe);
+ free(dupe);
+ free(normalized);
+ DECREF(source);
+ if (comparison != 0) {
+ FAIL(batch, "Not fully normalized");
+ return;
+ }
+ }
+ PASS(batch, "Normalization successful.");
+}
+
+void
+TestStrHelp_run_tests(TestStringHelper *self) {
+ TestBatch *batch = (TestBatch*)self;
+ test_overlap(batch);
+ test_to_base36(batch);
+ test_utf8_round_trip(batch);
+ test_utf8_valid(batch);
+ test_is_whitespace(batch);
+ test_back_utf8_char(batch);
+ test_utf8proc_normalization(batch);
+}
+
+
+
http://git-wip-us.apache.org/repos/asf/lucy/blob/9f826b31/core/Clownfish/Test/Util/TestStringHelper.cfh
----------------------------------------------------------------------
diff --git a/core/Clownfish/Test/Util/TestStringHelper.cfh b/core/Clownfish/Test/Util/TestStringHelper.cfh
new file mode 100644
index 0000000..1c33ea5
--- /dev/null
+++ b/core/Clownfish/Test/Util/TestStringHelper.cfh
@@ -0,0 +1,32 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+parcel TestClownfish;
+
+class Clownfish::Test::Util::TestStringHelper cnick TestStrHelp
+ inherits Clownfish::Test::TestBatch {
+
+ inert incremented TestStringHelper*
+ new(TestFormatter *formatter);
+
+ inert TestStringHelper*
+ init(TestStringHelper *self, TestFormatter *formatter);
+
+ void
+ Run_Tests(TestStringHelper *self);
+}
+
+
http://git-wip-us.apache.org/repos/asf/lucy/blob/9f826b31/core/Lucy/Test.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Test.c b/core/Lucy/Test.c
index e9604e9..19fef0e 100644
--- a/core/Lucy/Test.c
+++ b/core/Lucy/Test.c
@@ -42,15 +42,7 @@
#include "Lucy/Test/Index/TestSnapshot.h"
#include "Lucy/Test/Index/TestTermInfo.h"
#include "Lucy/Test/Object/TestBitVector.h"
-#include "Lucy/Test/Object/TestByteBuf.h"
-#include "Lucy/Test/Object/TestCharBuf.h"
-#include "Lucy/Test/Object/TestErr.h"
-#include "Lucy/Test/Object/TestHash.h"
#include "Lucy/Test/Object/TestI32Array.h"
-#include "Lucy/Test/Object/TestLockFreeRegistry.h"
-#include "Lucy/Test/Object/TestNum.h"
-#include "Lucy/Test/Object/TestObj.h"
-#include "Lucy/Test/Object/TestVArray.h"
#include "Lucy/Test/Plan/TestBlobType.h"
#include "Lucy/Test/Plan/TestFieldMisc.h"
#include "Lucy/Test/Plan/TestFieldType.h"
@@ -84,14 +76,10 @@
#include "Lucy/Test/Store/TestRAMFileHandle.h"
#include "Lucy/Test/Store/TestRAMFolder.h"
#include "Lucy/Test/TestSchema.h"
-#include "Lucy/Test/Util/TestAtomic.h"
#include "Lucy/Test/Util/TestIndexFileNames.h"
#include "Lucy/Test/Util/TestJson.h"
-#include "Lucy/Test/Util/TestMemory.h"
#include "Lucy/Test/Util/TestMemoryPool.h"
-#include "Lucy/Test/Util/TestNumberUtils.h"
#include "Lucy/Test/Util/TestPriorityQueue.h"
-#include "Lucy/Test/Util/TestStringHelper.h"
static void
S_unbuffer_stdout();
@@ -102,22 +90,10 @@ S_all_test_batches(TestFormatter *formatter) {
VA_Push(batches, (Obj*)TestPriQ_new(formatter));
VA_Push(batches, (Obj*)TestBitVector_new(formatter));
- VA_Push(batches, (Obj*)TestVArray_new(formatter));
- VA_Push(batches, (Obj*)TestHash_new(formatter));
- VA_Push(batches, (Obj*)TestObj_new(formatter));
- VA_Push(batches, (Obj*)TestErr_new(formatter));
- VA_Push(batches, (Obj*)TestBB_new(formatter));
VA_Push(batches, (Obj*)TestMemPool_new(formatter));
- VA_Push(batches, (Obj*)TestCB_new(formatter));
- VA_Push(batches, (Obj*)TestNumUtil_new(formatter));
- VA_Push(batches, (Obj*)TestNum_new(formatter));
- VA_Push(batches, (Obj*)TestStrHelp_new(formatter));
VA_Push(batches, (Obj*)TestIxFileNames_new(formatter));
VA_Push(batches, (Obj*)TestJson_new(formatter));
VA_Push(batches, (Obj*)TestI32Arr_new(formatter));
- VA_Push(batches, (Obj*)TestAtomic_new(formatter));
- VA_Push(batches, (Obj*)TestLFReg_new(formatter));
- VA_Push(batches, (Obj*)TestMemory_new(formatter));
VA_Push(batches, (Obj*)TestRAMFH_new(formatter));
VA_Push(batches, (Obj*)TestFSFH_new(formatter));
VA_Push(batches, (Obj*)TestInStream_new(formatter));
http://git-wip-us.apache.org/repos/asf/lucy/blob/9f826b31/core/Lucy/Test/Object/TestByteBuf.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Test/Object/TestByteBuf.c b/core/Lucy/Test/Object/TestByteBuf.c
deleted file mode 100644
index 9143b56..0000000
--- a/core/Lucy/Test/Object/TestByteBuf.c
+++ /dev/null
@@ -1,168 +0,0 @@
-/* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#define C_LUCY_TESTBYTEBUF
-#include "Lucy/Util/ToolSet.h"
-
-#include "Clownfish/Test/TestFormatter.h"
-#include "Lucy/Test.h"
-#include "Lucy/Test/TestUtils.h"
-#include "Lucy/Test/Object/TestByteBuf.h"
-
-TestByteBuf*
-TestBB_new(TestFormatter *formatter) {
- TestByteBuf *self = (TestByteBuf*)VTable_Make_Obj(TESTBYTEBUF);
- return TestBB_init(self, formatter);
-}
-
-TestByteBuf*
-TestBB_init(TestByteBuf *self, TestFormatter *formatter) {
- return (TestByteBuf*)TestBatch_init((TestBatch*)self, 22, formatter);
-}
-
-static void
-test_Equals(TestBatch *batch) {
- ByteBuf *wanted = BB_new_bytes("foo", 4); // Include terminating NULL.
- ByteBuf *got = BB_new_bytes("foo", 4);
-
- TEST_TRUE(batch, BB_Equals(wanted, (Obj*)got), "Equals");
- TEST_INT_EQ(batch, BB_Hash_Sum(got), BB_Hash_Sum(wanted), "Hash_Sum");
-
- TEST_TRUE(batch, BB_Equals_Bytes(got, "foo", 4), "Equals_Bytes");
- TEST_FALSE(batch, BB_Equals_Bytes(got, "foo", 3),
- "Equals_Bytes spoiled by different size");
- TEST_FALSE(batch, BB_Equals_Bytes(got, "bar", 4),
- "Equals_Bytes spoiled by different content");
-
- BB_Set_Size(got, 3);
- TEST_FALSE(batch, BB_Equals(wanted, (Obj*)got),
- "Different size spoils Equals");
- TEST_FALSE(batch, BB_Hash_Sum(got) == BB_Hash_Sum(wanted),
- "Different size spoils Hash_Sum (probably -- at least this one)");
-
- BB_Mimic_Bytes(got, "bar", 4);
- TEST_INT_EQ(batch, BB_Get_Size(wanted), BB_Get_Size(got),
- "same length");
- TEST_FALSE(batch, BB_Equals(wanted, (Obj*)got),
- "Different content spoils Equals");
-
- DECREF(got);
- DECREF(wanted);
-}
-
-static void
-test_Grow(TestBatch *batch) {
- ByteBuf *bb = BB_new(1);
- TEST_INT_EQ(batch, BB_Get_Capacity(bb), 8,
- "Allocate in 8-byte increments");
- BB_Grow(bb, 9);
- TEST_INT_EQ(batch, BB_Get_Capacity(bb), 16,
- "Grow in 8-byte increments");
- DECREF(bb);
-}
-
-static void
-test_Clone(TestBatch *batch) {
- ByteBuf *bb = BB_new_bytes("foo", 3);
- ByteBuf *twin = BB_Clone(bb);
- TEST_TRUE(batch, BB_Equals(bb, (Obj*)twin), "Clone");
- DECREF(bb);
- DECREF(twin);
-}
-
-static void
-test_compare(TestBatch *batch) {
- ByteBuf *a = BB_new_bytes("foo\0a", 5);
- ByteBuf *b = BB_new_bytes("foo\0b", 5);
-
- BB_Set_Size(a, 4);
- BB_Set_Size(b, 4);
- TEST_INT_EQ(batch, BB_compare(&a, &b), 0,
- "BB_compare returns 0 for equal ByteBufs");
-
- BB_Set_Size(a, 3);
- TEST_TRUE(batch, BB_compare(&a, &b) < 0, "shorter ByteBuf sorts first");
-
- BB_Set_Size(a, 5);
- BB_Set_Size(b, 5);
- TEST_TRUE(batch, BB_compare(&a, &b) < 0,
- "NULL doesn't interfere with BB_compare");
-
- DECREF(a);
- DECREF(b);
-}
-
-static void
-test_Mimic(TestBatch *batch) {
- ByteBuf *a = BB_new_bytes("foo", 3);
- ByteBuf *b = BB_new(0);
-
- BB_Mimic(b, (Obj*)a);
- TEST_TRUE(batch, BB_Equals(a, (Obj*)b), "Mimic");
-
- BB_Mimic_Bytes(a, "bar", 4);
- TEST_TRUE(batch, strcmp(BB_Get_Buf(a), "bar") == 0,
- "Mimic_Bytes content");
- TEST_INT_EQ(batch, BB_Get_Size(a), 4, "Mimic_Bytes size");
-
- BB_Mimic(b, (Obj*)a);
- TEST_TRUE(batch, BB_Equals(a, (Obj*)b), "Mimic");
-
- DECREF(a);
- DECREF(b);
-}
-
-static void
-test_Cat(TestBatch *batch) {
- ByteBuf *wanted = BB_new_bytes("foobar", 6);
- ByteBuf *got = BB_new_bytes("foo", 3);
- ByteBuf *scratch = BB_new_bytes("bar", 3);
-
- BB_Cat(got, scratch);
- TEST_TRUE(batch, BB_Equals(wanted, (Obj*)got), "Cat");
-
- BB_Mimic_Bytes(wanted, "foobarbaz", 9);
- BB_Cat_Bytes(got, "baz", 3);
- TEST_TRUE(batch, BB_Equals(wanted, (Obj*)got), "Cat_Bytes");
-
- DECREF(scratch);
- DECREF(got);
- DECREF(wanted);
-}
-
-static void
-test_serialization(TestBatch *batch) {
- ByteBuf *wanted = BB_new_bytes("foobar", 6);
- ByteBuf *got = (ByteBuf*)TestUtils_freeze_thaw((Obj*)wanted);
- TEST_TRUE(batch, got && BB_Equals(wanted, (Obj*)got),
- "Serialization round trip");
- DECREF(wanted);
- DECREF(got);
-}
-
-void
-TestBB_run_tests(TestByteBuf *self) {
- TestBatch *batch = (TestBatch*)self;
- test_Equals(batch);
- test_Grow(batch);
- test_Clone(batch);
- test_compare(batch);
- test_Mimic(batch);
- test_Cat(batch);
- test_serialization(batch);
-}
-
-
http://git-wip-us.apache.org/repos/asf/lucy/blob/9f826b31/core/Lucy/Test/Object/TestByteBuf.cfh
----------------------------------------------------------------------
diff --git a/core/Lucy/Test/Object/TestByteBuf.cfh b/core/Lucy/Test/Object/TestByteBuf.cfh
deleted file mode 100644
index ad4fabe..0000000
--- a/core/Lucy/Test/Object/TestByteBuf.cfh
+++ /dev/null
@@ -1,33 +0,0 @@
-/* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-parcel Lucy;
-parcel Lucy;
-
-class Lucy::Test::Object::TestByteBuf cnick TestBB
- inherits Clownfish::Test::TestBatch {
-
- inert incremented TestByteBuf*
- new(TestFormatter *formatter);
-
- inert TestByteBuf*
- init(TestByteBuf *self, TestFormatter *formatter);
-
- void
- Run_Tests(TestByteBuf *self);
-}
-
-
http://git-wip-us.apache.org/repos/asf/lucy/blob/9f826b31/core/Lucy/Test/Object/TestCharBuf.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Test/Object/TestCharBuf.c b/core/Lucy/Test/Object/TestCharBuf.c
deleted file mode 100644
index 1e95345..0000000
--- a/core/Lucy/Test/Object/TestCharBuf.c
+++ /dev/null
@@ -1,436 +0,0 @@
-/* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#define C_LUCY_TESTCHARBUF
-#include "Lucy/Util/ToolSet.h"
-#include <string.h>
-#include <stdio.h>
-
-#include "Clownfish/Test/TestFormatter.h"
-#include "Lucy/Test.h"
-#include "Lucy/Test/TestUtils.h"
-#include "Lucy/Test/Object/TestCharBuf.h"
-
-static char smiley[] = { (char)0xE2, (char)0x98, (char)0xBA, 0 };
-static uint32_t smiley_len = 3;
-
-TestCharBuf*
-TestCB_new(TestFormatter *formatter) {
- TestCharBuf *self = (TestCharBuf*)VTable_Make_Obj(TESTCHARBUF);
- return TestCB_init(self, formatter);
-}
-
-TestCharBuf*
-TestCB_init(TestCharBuf *self, TestFormatter *formatter) {
- return (TestCharBuf*)TestBatch_init((TestBatch*)self, 55, formatter);
-}
-
-static CharBuf*
-S_get_cb(const char *string) {
- return CB_new_from_utf8(string, strlen(string));
-}
-
-static void
-test_Cat(TestBatch *batch) {
- CharBuf *wanted = CB_newf("a%s", smiley);
- CharBuf *got = S_get_cb("");
-
- CB_Cat(got, wanted);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "Cat");
- DECREF(got);
-
- got = S_get_cb("a");
- CB_Cat_Char(got, 0x263A);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "Cat_Char");
- DECREF(got);
-
- got = S_get_cb("a");
- CB_Cat_Str(got, smiley, smiley_len);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "Cat_Str");
- DECREF(got);
-
- got = S_get_cb("a");
- CB_Cat_Trusted_Str(got, smiley, smiley_len);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "Cat_Trusted_Str");
- DECREF(got);
-
- DECREF(wanted);
-}
-
-static void
-test_Mimic_and_Clone(TestBatch *batch) {
- CharBuf *wanted = S_get_cb("foo");
- CharBuf *got = S_get_cb("bar");
-
- CB_Mimic(got, (Obj*)wanted);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "Mimic");
- DECREF(got);
-
- got = S_get_cb("bar");
- CB_Mimic_Str(got, "foo", 3);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "Mimic_Str");
- DECREF(got);
-
- got = CB_Clone(wanted);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "Clone");
- DECREF(got);
-
- DECREF(wanted);
-}
-
-static void
-test_Find(TestBatch *batch) {
- CharBuf *string = CB_new(10);
- CharBuf *substring = S_get_cb("foo");
-
- TEST_TRUE(batch, CB_Find(string, substring) == -1, "Not in empty string");
- CB_setf(string, "foo");
- TEST_TRUE(batch, CB_Find(string, substring) == 0, "Find complete string");
- CB_setf(string, "afoo");
- TEST_TRUE(batch, CB_Find(string, substring) == 1, "Find after first");
- CB_Set_Size(string, 3);
- TEST_TRUE(batch, CB_Find(string, substring) == -1, "Don't overrun");
- CB_setf(string, "afood");
- TEST_TRUE(batch, CB_Find(string, substring) == 1, "Find in middle");
-
- DECREF(substring);
- DECREF(string);
-}
-
-static void
-test_Code_Point_At_and_From(TestBatch *batch) {
- uint32_t code_points[] = { 'a', 0x263A, 0x263A, 'b', 0x263A, 'c' };
- uint32_t num_code_points = sizeof(code_points) / sizeof(uint32_t);
- CharBuf *string = CB_newf("a%s%sb%sc", smiley, smiley, smiley);
- uint32_t i;
-
- for (i = 0; i < num_code_points; i++) {
- uint32_t from = num_code_points - i - 1;
- TEST_INT_EQ(batch, CB_Code_Point_At(string, i), code_points[i],
- "Code_Point_At %ld", (long)i);
- TEST_INT_EQ(batch, CB_Code_Point_At(string, from),
- code_points[from], "Code_Point_From %ld", (long)from);
- }
-
- DECREF(string);
-}
-
-static void
-test_SubString(TestBatch *batch) {
- CharBuf *string = CB_newf("a%s%sb%sc", smiley, smiley, smiley);
- CharBuf *wanted = CB_newf("%sb%s", smiley, smiley);
- CharBuf *got = CB_SubString(string, 2, 3);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "SubString");
- DECREF(wanted);
- DECREF(got);
- DECREF(string);
-}
-
-static void
-test_Nip_and_Chop(TestBatch *batch) {
- CharBuf *wanted;
- CharBuf *got;
-
- wanted = CB_newf("%sb%sc", smiley, smiley);
- got = CB_newf("a%s%sb%sc", smiley, smiley, smiley);
- CB_Nip(got, 2);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "Nip");
- DECREF(wanted);
- DECREF(got);
-
- wanted = CB_newf("a%s%s", smiley, smiley);
- got = CB_newf("a%s%sb%sc", smiley, smiley, smiley);
- CB_Chop(got, 3);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "Chop");
- DECREF(wanted);
- DECREF(got);
-}
-
-
-static void
-test_Truncate(TestBatch *batch) {
- CharBuf *wanted = CB_newf("a%s", smiley, smiley);
- CharBuf *got = CB_newf("a%s%sb%sc", smiley, smiley, smiley);
- CB_Truncate(got, 2);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "Truncate");
- DECREF(wanted);
- DECREF(got);
-}
-
-static void
-test_Trim(TestBatch *batch) {
- uint32_t spaces[] = {
- ' ', '\t', '\r', '\n', 0x000B, 0x000C, 0x000D, 0x0085,
- 0x00A0, 0x1680, 0x180E, 0x2000, 0x2001, 0x2002, 0x2003, 0x2004,
- 0x2005, 0x2006, 0x2007, 0x2008, 0x2009, 0x200A, 0x2028, 0x2029,
- 0x202F, 0x205F, 0x3000
- };
- uint32_t num_spaces = sizeof(spaces) / sizeof(uint32_t);
- uint32_t i;
- CharBuf *got = CB_new(0);
-
- // Surround a smiley with lots of whitespace.
- for (i = 0; i < num_spaces; i++) { CB_Cat_Char(got, spaces[i]); }
- CB_Cat_Char(got, 0x263A);
- for (i = 0; i < num_spaces; i++) { CB_Cat_Char(got, spaces[i]); }
-
- TEST_TRUE(batch, CB_Trim_Top(got), "Trim_Top returns true on success");
- TEST_FALSE(batch, CB_Trim_Top(got),
- "Trim_Top returns false on failure");
- TEST_TRUE(batch, CB_Trim_Tail(got), "Trim_Tail returns true on success");
- TEST_FALSE(batch, CB_Trim_Tail(got),
- "Trim_Tail returns false on failure");
- TEST_TRUE(batch, CB_Equals_Str(got, smiley, smiley_len),
- "Trim_Top and Trim_Tail worked");
-
- // Build the spacey smiley again.
- CB_Truncate(got, 0);
- for (i = 0; i < num_spaces; i++) { CB_Cat_Char(got, spaces[i]); }
- CB_Cat_Char(got, 0x263A);
- for (i = 0; i < num_spaces; i++) { CB_Cat_Char(got, spaces[i]); }
-
- TEST_TRUE(batch, CB_Trim(got), "Trim returns true on success");
- TEST_FALSE(batch, CB_Trim(got), "Trim returns false on failure");
- TEST_TRUE(batch, CB_Equals_Str(got, smiley, smiley_len),
- "Trim worked");
-
- DECREF(got);
-}
-
-static void
-test_To_F64(TestBatch *batch) {
- CharBuf *charbuf = S_get_cb("1.5");
- double difference = 1.5 - CB_To_F64(charbuf);
- if (difference < 0) { difference = 0 - difference; }
- TEST_TRUE(batch, difference < 0.001, "To_F64");
-
- CB_setf(charbuf, "-1.5");
- difference = 1.5 + CB_To_F64(charbuf);
- if (difference < 0) { difference = 0 - difference; }
- TEST_TRUE(batch, difference < 0.001, "To_F64 negative");
-
- CB_setf(charbuf, "1.59");
- double value_full = CB_To_F64(charbuf);
- CB_Set_Size(charbuf, 3);
- double value_short = CB_To_F64(charbuf);
- TEST_TRUE(batch, value_short < value_full,
- "TO_F64 doesn't run past end of string");
-
- DECREF(charbuf);
-}
-
-static void
-test_To_I64(TestBatch *batch) {
- CharBuf *charbuf = S_get_cb("10");
- TEST_TRUE(batch, CB_To_I64(charbuf) == 10, "To_I64");
- CB_setf(charbuf, "-10");
- TEST_TRUE(batch, CB_To_I64(charbuf) == -10, "To_I64 negative");
- DECREF(charbuf);
-}
-
-
-static void
-test_vcatf_s(TestBatch *batch) {
- CharBuf *wanted = S_get_cb("foo bar bizzle baz");
- CharBuf *got = S_get_cb("foo ");
- CB_catf(got, "bar %s baz", "bizzle");
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%s");
- DECREF(wanted);
- DECREF(got);
-}
-
-static void
-test_vcatf_null_string(TestBatch *batch) {
- CharBuf *wanted = S_get_cb("foo bar [NULL] baz");
- CharBuf *got = S_get_cb("foo ");
- CB_catf(got, "bar %s baz", NULL);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%s NULL");
- DECREF(wanted);
- DECREF(got);
-}
-
-static void
-test_vcatf_cb(TestBatch *batch) {
- CharBuf *wanted = S_get_cb("foo bar ZEKE baz");
- CharBuf *catworthy = S_get_cb("ZEKE");
- CharBuf *got = S_get_cb("foo ");
- CB_catf(got, "bar %o baz", catworthy);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%o CharBuf");
- DECREF(catworthy);
- DECREF(wanted);
- DECREF(got);
-}
-
-static void
-test_vcatf_obj(TestBatch *batch) {
- CharBuf *wanted = S_get_cb("ooga 20 booga");
- Integer32 *i32 = Int32_new(20);
- CharBuf *got = S_get_cb("ooga");
- CB_catf(got, " %o booga", i32);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%o Obj");
- DECREF(i32);
- DECREF(wanted);
- DECREF(got);
-}
-
-static void
-test_vcatf_null_obj(TestBatch *batch) {
- CharBuf *wanted = S_get_cb("foo bar [NULL] baz");
- CharBuf *got = S_get_cb("foo ");
- CB_catf(got, "bar %o baz", NULL);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%o NULL");
- DECREF(wanted);
- DECREF(got);
-}
-
-static void
-test_vcatf_i8(TestBatch *batch) {
- CharBuf *wanted = S_get_cb("foo bar -3 baz");
- int8_t num = -3;
- CharBuf *got = S_get_cb("foo ");
- CB_catf(got, "bar %i8 baz", num);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%i8");
- DECREF(wanted);
- DECREF(got);
-}
-
-static void
-test_vcatf_i32(TestBatch *batch) {
- CharBuf *wanted = S_get_cb("foo bar -100000 baz");
- int32_t num = -100000;
- CharBuf *got = S_get_cb("foo ");
- CB_catf(got, "bar %i32 baz", num);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%i32");
- DECREF(wanted);
- DECREF(got);
-}
-
-static void
-test_vcatf_i64(TestBatch *batch) {
- CharBuf *wanted = S_get_cb("foo bar -5000000000 baz");
- int64_t num = INT64_C(-5000000000);
- CharBuf *got = S_get_cb("foo ");
- CB_catf(got, "bar %i64 baz", num);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%i64");
- DECREF(wanted);
- DECREF(got);
-}
-
-static void
-test_vcatf_u8(TestBatch *batch) {
- CharBuf *wanted = S_get_cb("foo bar 3 baz");
- uint8_t num = 3;
- CharBuf *got = S_get_cb("foo ");
- CB_catf(got, "bar %u8 baz", num);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%u8");
- DECREF(wanted);
- DECREF(got);
-}
-
-static void
-test_vcatf_u32(TestBatch *batch) {
- CharBuf *wanted = S_get_cb("foo bar 100000 baz");
- uint32_t num = 100000;
- CharBuf *got = S_get_cb("foo ");
- CB_catf(got, "bar %u32 baz", num);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%u32");
- DECREF(wanted);
- DECREF(got);
-}
-
-static void
-test_vcatf_u64(TestBatch *batch) {
- CharBuf *wanted = S_get_cb("foo bar 5000000000 baz");
- uint64_t num = UINT64_C(5000000000);
- CharBuf *got = S_get_cb("foo ");
- CB_catf(got, "bar %u64 baz", num);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%u64");
- DECREF(wanted);
- DECREF(got);
-}
-
-static void
-test_vcatf_f64(TestBatch *batch) {
- CharBuf *wanted;
- char buf[64];
- float num = 1.3f;
- CharBuf *got = S_get_cb("foo ");
- sprintf(buf, "foo bar %g baz", num);
- wanted = CB_new_from_trusted_utf8(buf, strlen(buf));
- CB_catf(got, "bar %f64 baz", num);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%f64");
- DECREF(wanted);
- DECREF(got);
-}
-
-static void
-test_vcatf_x32(TestBatch *batch) {
- CharBuf *wanted;
- char buf[64];
- unsigned long num = INT32_MAX;
- CharBuf *got = S_get_cb("foo ");
-#if (SIZEOF_LONG == 4)
- sprintf(buf, "foo bar %.8lx baz", num);
-#elif (SIZEOF_INT == 4)
- sprintf(buf, "foo bar %.8x baz", (unsigned)num);
-#endif
- wanted = CB_new_from_trusted_utf8(buf, strlen(buf));
- CB_catf(got, "bar %x32 baz", (uint32_t)num);
- TEST_TRUE(batch, CB_Equals(wanted, (Obj*)got), "%%x32");
- DECREF(wanted);
- DECREF(got);
-}
-
-static void
-test_serialization(TestBatch *batch) {
- CharBuf *wanted = S_get_cb("foo");
- CharBuf *got = (CharBuf*)TestUtils_freeze_thaw((Obj*)wanted);
- TEST_TRUE(batch, got && CB_Equals(wanted, (Obj*)got),
- "Round trip through FREEZE/THAW");
- DECREF(got);
- DECREF(wanted);
-}
-
-void
-TestCB_run_tests(TestCharBuf *self) {
- TestBatch *batch = (TestBatch*)self;
- test_vcatf_s(batch);
- test_vcatf_null_string(batch);
- test_vcatf_cb(batch);
- test_vcatf_obj(batch);
- test_vcatf_null_obj(batch);
- test_vcatf_i8(batch);
- test_vcatf_i32(batch);
- test_vcatf_i64(batch);
- test_vcatf_u8(batch);
- test_vcatf_u32(batch);
- test_vcatf_u64(batch);
- test_vcatf_f64(batch);
- test_vcatf_x32(batch);
- test_Cat(batch);
- test_Mimic_and_Clone(batch);
- test_Code_Point_At_and_From(batch);
- test_Find(batch);
- test_SubString(batch);
- test_Nip_and_Chop(batch);
- test_Truncate(batch);
- test_Trim(batch);
- test_To_F64(batch);
- test_To_I64(batch);
- test_serialization(batch);
-}
-
-
http://git-wip-us.apache.org/repos/asf/lucy/blob/9f826b31/core/Lucy/Test/Object/TestCharBuf.cfh
----------------------------------------------------------------------
diff --git a/core/Lucy/Test/Object/TestCharBuf.cfh b/core/Lucy/Test/Object/TestCharBuf.cfh
deleted file mode 100644
index 0e70e40..0000000
--- a/core/Lucy/Test/Object/TestCharBuf.cfh
+++ /dev/null
@@ -1,33 +0,0 @@
-/* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-parcel Lucy;
-parcel Lucy;
-
-class Lucy::Test::Object::TestCharBuf cnick TestCB
- inherits Clownfish::Test::TestBatch {
-
- inert incremented TestCharBuf*
- new(TestFormatter *formatter);
-
- inert TestCharBuf*
- init(TestCharBuf *self, TestFormatter *formatter);
-
- void
- Run_Tests(TestCharBuf *self);
-}
-
-
http://git-wip-us.apache.org/repos/asf/lucy/blob/9f826b31/core/Lucy/Test/Object/TestErr.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Test/Object/TestErr.c b/core/Lucy/Test/Object/TestErr.c
deleted file mode 100644
index 5cbd365..0000000
--- a/core/Lucy/Test/Object/TestErr.c
+++ /dev/null
@@ -1,53 +0,0 @@
-/* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "Lucy/Util/ToolSet.h"
-
-#include "Clownfish/Test/TestFormatter.h"
-#include "Lucy/Test.h"
-#include "Lucy/Test/TestUtils.h"
-#include "Lucy/Test/Object/TestErr.h"
-#include "Clownfish/Err.h"
-
-TestErr*
-TestErr_new(TestFormatter *formatter) {
- TestErr *self = (TestErr*)VTable_Make_Obj(TESTERR);
- return TestErr_init(self, formatter);
-}
-
-TestErr*
-TestErr_init(TestErr *self, TestFormatter *formatter) {
- return (TestErr*)TestBatch_init((TestBatch*)self, 1, formatter);
-}
-
-static void
-test_To_String(TestBatch *batch) {
- CharBuf *message = CB_newf("oops");
- Err *error = Err_new(message);
- CharBuf *string = Err_To_String(error);
- TEST_TRUE(batch, CB_Equals(message, (Obj*)string),
- "Stringifies as message");
- DECREF(string);
- DECREF(error);
-}
-
-void
-TestErr_run_tests(TestErr *self) {
- TestBatch *batch = (TestBatch*)self;
- test_To_String(batch);
-}
-
-
http://git-wip-us.apache.org/repos/asf/lucy/blob/9f826b31/core/Lucy/Test/Object/TestErr.cfh
----------------------------------------------------------------------
diff --git a/core/Lucy/Test/Object/TestErr.cfh b/core/Lucy/Test/Object/TestErr.cfh
deleted file mode 100644
index 316512f..0000000
--- a/core/Lucy/Test/Object/TestErr.cfh
+++ /dev/null
@@ -1,31 +0,0 @@
-/* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-parcel Lucy;
-
-class Lucy::Test::Object::TestErr
- inherits Clownfish::Test::TestBatch {
-
- inert incremented TestErr*
- new(TestFormatter *formatter);
-
- inert TestErr*
- init(TestErr *self, TestFormatter *formatter);
-
- void
- Run_Tests(TestErr *self);
-}
-