[Groonga-commit] groonga/groonga [master] Add tests of grn_tiny_array.

Back to archive index

null+****@clear***** null+****@clear*****
2012年 3月 27日 (火) 17:53:34 JST


Susumu Yata	2012-03-27 17:53:34 +0900 (Tue, 27 Mar 2012)

  New Revision: a96bcb65b9ab4528e46e2f5579e3cfc2506cbb52

  Log:
    Add tests of grn_tiny_array.

  Added files:
    test/unit/core/test-tiny-array.c
  Modified files:
    test/unit/core/Makefile.am

  Modified: test/unit/core/Makefile.am (+1 -0)
===================================================================
--- test/unit/core/Makefile.am    2012-03-27 15:47:39 +0900 (3ce79cf)
+++ test/unit/core/Makefile.am    2012-03-27 17:53:34 +0900 (a219280)
@@ -4,6 +4,7 @@ SUBDIRS =					\
 if WITH_CUTTER
 noinst_LTLIBRARIES =				\
 	test-context.la				\
+	test-tiny-array.la			\
 	test-hash.la				\
 	test-hash-sort.la			\
 	test-hash-cursor.la			\

  Added: test/unit/core/test-tiny-array.c (+131 -0) 100644
===================================================================
--- /dev/null
+++ test/unit/core/test-tiny-array.c    2012-03-27 17:53:34 +0900 (6fd0ab5)
@@ -0,0 +1,131 @@
+/* -*- c-basic-offset: 2; coding: utf-8 -*- */
+/*
+  Copyright (C) 2012  Brazil Inc.
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License version 2.1 as published by the Free Software Foundation.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#include <gcutter.h>
+#include <glib/gstdio.h>
+
+#include "../lib/grn-assertions.h"
+
+#include <string.h>
+
+#include <hash.h>
+
+static grn_ctx ctx;
+
+void
+cut_setup(void)
+{
+  grn_ctx_init(&ctx, 0);
+}
+
+void
+cut_teardown(void)
+{
+  grn_ctx_fin(&ctx);
+}
+
+void
+test_basic_operations(uint16_t element_size, uint16_t flags)
+{
+  grn_id id;
+  grn_tiny_array array;
+  grn_tiny_array_init(&ctx, &array, element_size, flags);
+  cut_assert_null(grn_tiny_array_at(&array, 0));
+  for (id = 1; id < (1 << 8); id++) {
+    void * const ptr = grn_tiny_array_at(&array, id);
+    cut_assert_not_null(ptr);
+    memset(ptr, (unsigned char)id, element_size);
+  }
+  for (id = 1; id < (1 << 8); id++) {
+    uint16_t byte_id;
+    const unsigned char expected_byte = (unsigned char)id;
+    const unsigned char * const ptr =
+        (const unsigned char *)grn_tiny_array_at(&array, id);
+    for (byte_id = 0; byte_id < element_size; byte_id++) {
+      cut_assert_equal_int(expected_byte, ptr[byte_id]);
+    }
+  }
+  grn_tiny_array_fin(&array);
+}
+
+void
+test_at(void)
+{
+  uint16_t element_size;
+  for (element_size = 1; element_size < 16; element_size++) {
+    test_basic_operations(element_size, 0);
+  }
+}
+
+void
+test_id(void)
+{
+  grn_id id;
+  grn_tiny_array array;
+  grn_tiny_array_init(&ctx, &array, sizeof(int), GRN_TINY_ARRAY_CLEAR);
+  grn_test_assert_equal_id(&ctx, (grn_id)0, grn_tiny_array_id(&array, NULL));
+  for (id = 1; id < (1 << 10); id++) {
+    const void * const ptr = grn_tiny_array_at(&array, id);
+    cut_assert_not_null(ptr);
+    grn_test_assert_equal_id(&ctx, (grn_id)id, grn_tiny_array_id(&array, ptr));
+  }
+  grn_tiny_array_fin(&array);
+}
+
+void
+test_clear(void)
+{
+  grn_id id;
+  grn_tiny_array array;
+  grn_tiny_array_init(&ctx, &array, sizeof(int), GRN_TINY_ARRAY_CLEAR);
+  for (id = 1; id < (1 << 10); id++) {
+    const int expected_value = 0;
+    const int * const value_ptr = (const int *)grn_tiny_array_at(&array, id);
+    cut_assert_not_null(value_ptr);
+    cut_assert_equal_memory(&expected_value, sizeof(expected_value),
+                            value_ptr, sizeof(int));
+  }
+  grn_tiny_array_fin(&array);
+
+  {
+    uint16_t element_size;
+    for (element_size = 1; element_size < 16; element_size++) {
+      test_basic_operations(element_size, GRN_TINY_ARRAY_CLEAR);
+    }
+  }
+}
+
+void
+test_threadsafe(void)
+{
+  /* TODO: A multi-threaded test should be done. */
+
+  uint16_t element_size;
+  for (element_size = 1; element_size < 16; element_size++) {
+    test_basic_operations(element_size, GRN_TINY_ARRAY_THREADSAFE);
+  }
+}
+
+void
+test_use_malloc(void)
+{
+  uint16_t element_size;
+  for (element_size = 1; element_size < 16; element_size++) {
+    test_basic_operations(element_size, GRN_TINY_ARRAY_USE_MALLOC);
+  }
+}




Groonga-commit メーリングリストの案内
Back to archive index