]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
unit test for util/regional.c.
authorWouter Wijngaards <wouter@nlnetlabs.nl>
Mon, 15 Mar 2010 13:18:49 +0000 (13:18 +0000)
committerWouter Wijngaards <wouter@nlnetlabs.nl>
Mon, 15 Mar 2010 13:18:49 +0000 (13:18 +0000)
git-svn-id: file:///svn/unbound/trunk@2037 be551aaa-1e26-0410-a405-d3ace91eadb9

doc/Changelog
testcode/unitmain.c
testcode/unitmain.h
testcode/unitregional.c [new file with mode: 0644]

index 69bfab2db4896714296f735235ba75fac59bd26e..7674ca206173d82e75bfcffb9fb88db63cfa568e 100644 (file)
@@ -1,3 +1,6 @@
+15 March 2010: Wouter
+       - unit test for util/regional.c.
+
 12 March 2010: Wouter
        - Reordered configure checks so fork and -lnsl -lsocket checks are
          earlier, and thus later checks benefit from and do not hinder them.
index 7af7d0f347ed6a5e3997672c0537040359d95054..4f7e8125a36e834a001f301417a7cd8e95bbc209 100644 (file)
@@ -505,6 +505,7 @@ main(int argc, char* argv[])
        rtt_test();
        anchors_test();
        alloc_test();
+       regional_test();
        lruhash_test();
        slabhash_test();
        infra_test();
index b77059d276dad5a516a5d2f320d6560c5e9b7c48..fb4319b18b60779d4f22fef291e1fda0c8f4110d 100644 (file)
@@ -70,5 +70,7 @@ void anchors_test();
 void verify_test();
 /** unit test for negative cache functions */
 void neg_test();
+/** unit test for regional allocator functions */
+void regional_test();
 
 #endif /* TESTCODE_UNITMAIN_H */
diff --git a/testcode/unitregional.c b/testcode/unitregional.c
new file mode 100644 (file)
index 0000000..3d9223f
--- /dev/null
@@ -0,0 +1,244 @@
+/*
+ * testcode/unitregional.c - unit test for regional allocator.
+ *
+ * Copyright (c) 2010, NLnet Labs. All rights reserved.
+ *
+ * This software is open source.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * Neither the name of the NLNET LABS nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+/**
+ * \file
+ * Tests the regional special purpose allocator.
+ */
+
+#include "config.h"
+#include "testcode/unitmain.h"
+#include "util/log.h"
+#include "util/regional.h"
+
+/** test regional corner cases, zero, one, end of structure */
+static void
+corner_cases(struct regional* r)
+{
+       size_t s; /* shadow count of allocated memory */
+       void* a;
+       size_t minsize = sizeof(uint64_t);
+       size_t mysize;
+       char* str;
+       unit_assert(r);
+       /* alloc cases:
+        * 0, 1, 2.
+        * smaller than LARGE_OBJECT_SIZE.
+        * smaller but does not fit in remainder in regional.
+        * smaller but exactly fits in remainder of regional.
+        * size is remainder of regional - 8.
+        * size is remainder of regional + 8.
+        * larger than LARGE_OBJECT_SIZE.
+        */
+       s = sizeof(struct regional);
+       unit_assert((s % minsize) == 0);
+       unit_assert(r->available == r->first_size - s);
+       unit_assert(r->large_list == NULL);
+       unit_assert(r->next == NULL);
+
+       /* Note an alloc of 0 gets a pointer to current last
+        * position (where you should then use 0 bytes) */
+       a = regional_alloc(r, 0);
+       unit_assert(a);
+       s+=0;
+       unit_assert(r->available == r->first_size - s);
+
+       a = regional_alloc(r, 1);
+       unit_assert(a);
+       memset(a, 0x42, 1);
+       s+=minsize;
+       unit_assert(r->available == r->first_size - s);
+
+       a = regional_alloc(r, 2);
+       unit_assert(a);
+       memset(a, 0x42, 2);
+       s+=minsize;
+       unit_assert(r->available == r->first_size - s);
+
+       a = regional_alloc(r, 128);
+       unit_assert(a);
+       memset(a, 0x42, 128);
+       s+=128;
+       unit_assert(r->available == r->first_size - s);
+
+       unit_assert(r->large_list == NULL);
+       a = regional_alloc(r, 10240);
+       unit_assert(a);
+       unit_assert(r->large_list != NULL);
+       memset(a, 0x42, 10240);
+       /* s does not change */
+       unit_assert(r->available == r->first_size - s);
+       unit_assert(r->total_large == 10240+minsize);
+
+       /* go towards the end of the current chunk */
+       while(r->available > 1024) {
+               a = regional_alloc(r, 1024);
+               unit_assert(a);
+               memset(a, 0x42, 1024);
+               s += 1024;
+               unit_assert(r->available == r->first_size - s);
+       }
+
+       unit_assert(r->next == NULL);
+       mysize = 1280; /* does not fit in current chunk */
+       a = regional_alloc(r, mysize);
+       memset(a, 0x42, mysize);
+       unit_assert(r->next != NULL);
+       unit_assert(a);
+
+       /* go towards the end of the current chunk */
+       while(r->available > 864) {
+               a = regional_alloc(r, 864);
+               unit_assert(a);
+               memset(a, 0x42, 864);
+               s += 864;
+       }
+
+       mysize = r->available; /* exactly fits */
+       a = regional_alloc(r, mysize);
+       memset(a, 0x42, mysize);
+       unit_assert(a);
+       unit_assert(r->available == 0); /* implementation does not go ahead*/
+
+       a = regional_alloc(r, 8192); /* another large allocation */
+       unit_assert(a);
+       memset(a, 0x42, 8192);
+       unit_assert(r->available == 0);
+       unit_assert(r->total_large == 10240 + 8192 + 2*minsize);
+
+       a = regional_alloc(r, 32); /* make new chunk */
+       unit_assert(a);
+       memset(a, 0x42, 32);
+       unit_assert(r->available > 0);
+       unit_assert(r->total_large == 10240 + 8192 + 2*minsize);
+
+       /* go towards the end of the current chunk */
+       while(r->available > 1320) {
+               a = regional_alloc(r, 1320);
+               unit_assert(a);
+               memset(a, 0x42, 1320);
+               s += 1320;
+       }
+
+       mysize = r->available + 8; /* exact + 8 ; does not fit */
+       a = regional_alloc(r, mysize);
+       memset(a, 0x42, mysize);
+       unit_assert(a);
+       unit_assert(r->available > 0); /* new chunk */
+
+       /* go towards the end of the current chunk */
+       while(r->available > 1480) {
+               a = regional_alloc(r, 1480);
+               unit_assert(a);
+               memset(a, 0x42, 1480);
+               s += 1480;
+       }
+
+       mysize = r->available - 8; /* exact - 8 ; fits. */
+       a = regional_alloc(r, mysize);
+       memset(a, 0x42, mysize);
+       unit_assert(a);
+       unit_assert(r->available == 8);
+
+       /* test if really copied over */
+       str = "test12345";
+       a = regional_alloc_init(r, str, 8);
+       unit_assert(a);
+       unit_assert(memcmp(a, str, 8) == 0);
+
+       /* test if really zeroed */
+       a = regional_alloc_zero(r, 32);
+       str="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
+       unit_assert(a);
+       unit_assert(memcmp(a, str, 32) == 0);
+
+       /* test if copied over (and null byte) */
+       str = "an interesting string";
+       a = regional_strdup(r, str);
+       unit_assert(a);
+       unit_assert(memcmp(a, str, strlen(str)+1) == 0);
+
+       regional_free_all(r);
+}
+
+/** test specific cases */
+static void
+specific_cases()
+{
+       struct regional* r = regional_create();
+       corner_cases(r);
+       regional_destroy(r);
+       r = regional_create_custom(2048); /* a small regional */
+       unit_assert(r->first_size == 2048);
+       unit_assert(regional_get_mem(r) == 2048);
+       corner_cases(r);
+       unit_assert(regional_get_mem(r) == 2048);
+       regional_destroy(r);
+}
+
+/** put random stuff in a region and free it */
+static void
+burden_test(size_t max)
+{
+       size_t get;
+       void* a;
+       int i;
+       struct regional* r = regional_create_custom(2048);
+       for(i=0; i<1000; i++) {
+               get = random() % max;
+               a = regional_alloc(r, get);
+               unit_assert(a);
+               memset(a, 0x54, get);
+       }
+       regional_free_all(r);
+       regional_destroy(r);
+}
+
+/** randomly allocate stuff */
+static void
+random_burden()
+{
+       size_t max_alloc = 2048 + 128; /* small chance of LARGE */
+       int i;
+       for(i=0; i<100; i++)
+               burden_test(max_alloc);
+}
+
+void regional_test()
+{
+       unit_show_feature("regional");
+       specific_cases();
+       random_burden();
+}