Improve Blob test coverage

Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/8242eb69
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/8242eb69
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/8242eb69

Branch: refs/heads/master
Commit: 8242eb6900a5ef6200341c3b5649d5cad33d1f74
Parents: 08c29a7
Author: Nick Wellnhofer <[email protected]>
Authored: Tue May 10 21:06:37 2016 +0200
Committer: Nick Wellnhofer <[email protected]>
Committed: Sun May 15 17:57:34 2016 +0200

----------------------------------------------------------------------
 runtime/core/Clownfish/Blob.c          |  4 +---
 runtime/core/Clownfish/Test/TestBlob.c | 34 ++++++++++++++++++++++++++++-
 2 files changed, 34 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/8242eb69/runtime/core/Clownfish/Blob.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/Blob.c b/runtime/core/Clownfish/Blob.c
index dec4954..becb910 100644
--- a/runtime/core/Clownfish/Blob.c
+++ b/runtime/core/Clownfish/Blob.c
@@ -33,9 +33,7 @@ Blob_new(const void *bytes, size_t size) {
 Blob*
 Blob_init(Blob *self, const void *bytes, size_t size) {
     char *copy = (char*)MALLOCATE(size);
-    if (size > 0) {
-        memcpy(copy, bytes, size);
-    }
+    memcpy(copy, bytes, size);
 
     self->buf      = copy;
     self->size     = size;

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/8242eb69/runtime/core/Clownfish/Test/TestBlob.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/Test/TestBlob.c 
b/runtime/core/Clownfish/Test/TestBlob.c
index c8c70c3..5776f47 100644
--- a/runtime/core/Clownfish/Test/TestBlob.c
+++ b/runtime/core/Clownfish/Test/TestBlob.c
@@ -24,6 +24,9 @@
 #include "Clownfish/TestHarness/TestBatchRunner.h"
 #include "Clownfish/TestHarness/TestUtils.h"
 #include "Clownfish/Class.h"
+#include "Clownfish/Util/Memory.h"
+
+#include <string.h>
 
 TestBlob*
 TestBlob_new() {
@@ -31,6 +34,28 @@ TestBlob_new() {
 }
 
 static void
+test_new_steal(TestBatchRunner *runner) {
+    size_t size = 4;
+    char *buf = (char*)MALLOCATE(size);
+    memset(buf, 'x', size);
+    Blob *blob = Blob_new_steal(buf, size);
+    TEST_TRUE(runner, Blob_Get_Buf(blob) == buf, "new_steal steals buf");
+    TEST_TRUE(runner, Blob_Equals_Bytes(blob, "xxxx", 4),
+              "new_steal doesn't change buf");
+    DECREF(blob);
+}
+
+static void
+test_new_wrap(TestBatchRunner *runner) {
+    static const char buf[] = "xxxx";
+    Blob *blob = Blob_new_wrap(buf, 4);
+    TEST_TRUE(runner, Blob_Get_Buf(blob) == buf, "new_wrap wraps buf");
+    TEST_TRUE(runner, Blob_Equals_Bytes(blob, "xxxx", 4),
+              "new_wrap doesn't change buf");
+    DECREF(blob);
+}
+
+static void
 test_Equals(TestBatchRunner *runner) {
     Blob *blob = Blob_new("foo", 4); // Include terminating NULL.
 
@@ -56,6 +81,9 @@ test_Equals(TestBatchRunner *runner) {
         DECREF(other);
     }
 
+    TEST_FALSE(runner, Blob_Equals(blob, (Obj*)BLOB),
+               "Different type spoils Equals");
+
     TEST_TRUE(runner, Blob_Equals_Bytes(blob, "foo", 4), "Equals_Bytes");
     TEST_FALSE(runner, Blob_Equals_Bytes(blob, "foo", 3),
                "Equals_Bytes spoiled by different size");
@@ -90,6 +118,8 @@ test_Compare_To(TestBatchRunner *runner) {
         Blob *b = Blob_new("foo\0b", 5);
         TEST_TRUE(runner, Blob_Compare_To(a, (Obj*)b) < 0,
                   "shorter Blob sorts first");
+        TEST_TRUE(runner, Blob_Compare_To(b, (Obj*)a) > 0,
+                  "longer Blob sorts last");
         DECREF(a);
         DECREF(b);
     }
@@ -106,7 +136,9 @@ test_Compare_To(TestBatchRunner *runner) {
 
 void
 TestBlob_Run_IMP(TestBlob *self, TestBatchRunner *runner) {
-    TestBatchRunner_Plan(runner, (TestBatch*)self, 11);
+    TestBatchRunner_Plan(runner, (TestBatch*)self, 17);
+    test_new_steal(runner);
+    test_new_wrap(runner);
     test_Equals(runner);
     test_Clone(runner);
     test_Compare_To(runner);

Reply via email to