summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkira TAGOH <akira@tagoh.org>2007-10-22 02:17:17 +0000
committerAkira TAGOH <akira@tagoh.org>2007-10-22 02:17:17 +0000
commitf401fb2fcc1e4ea157523063b558b8889bd70592 (patch)
tree8b68731a60734cd83d8104ae26e343e0ba5eb58e
parentd5fa7c116f91b39360aa99b110296674bc0ba7ed (diff)
2007-10-22 Akira TAGOH <akira@tagoh.org>
* hieroglyph/hgarray.c (hg_object_array_new): initialize the array. (hg_object_array_compare): consider the objects as the same if the same address is given. * hieroglyph/hgobject.c (hg_object_compare): consider the objects as the same if the same address is given. * tests/hgarray.c: add more test cases.
-rw-r--r--ChangeLog11
-rw-r--r--hieroglyph/hgarray.c9
-rw-r--r--hieroglyph/hgobject.c2
-rw-r--r--tests/hgarray.c131
4 files changed, 147 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index 6ca9d48..36ceb5f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2007-10-22 Akira TAGOH <akira@tagoh.org>
+
+ * hieroglyph/hgarray.c (hg_object_array_new): initialize the array.
+ (hg_object_array_compare): consider the objects as the same if the same
+ address is given.
+
+ * hieroglyph/hgobject.c (hg_object_compare): consider the objects as
+ the same if the same address is given.
+
+ * tests/hgarray.c: add more test cases.
+
2007-10-18 Akira TAGOH <akira@tagoh.org>
* tests/Makefile.am: add more test cases.
diff --git a/hieroglyph/hgarray.c b/hieroglyph/hgarray.c
index 6dfb589..fabcbd3 100644
--- a/hieroglyph/hgarray.c
+++ b/hieroglyph/hgarray.c
@@ -25,6 +25,7 @@
#include "config.h"
#endif
+#include <string.h>
#include <glib/gstrfuncs.h>
#include <glib/gthread.h>
#include <hieroglyph/hgobject.h>
@@ -57,10 +58,12 @@ hg_object_array_new(hg_vm_t *vm,
HG_OBJECT_ARRAY (retval)->real_length = length;
data = HG_OBJECT_ARRAY_DATA (retval);
data->name[0] = 0;
- if (length > 0)
+ if (length > 0) {
data->array = (hg_object_t *)data->data;
- else
+ memset(data->array, 0, sizeof (hg_object_t) * length);
+ } else {
data->array = NULL;
+ }
}
return retval;
@@ -164,6 +167,8 @@ hg_object_array_compare(hg_object_t *object1,
hg_return_val_if_fail (HG_OBJECT_IS_ARRAY (object1), FALSE);
hg_return_val_if_fail (HG_OBJECT_IS_ARRAY (object2), FALSE);
+ if (object1 == object2)
+ return TRUE;
if (HG_OBJECT_ARRAY (object1)->real_length != HG_OBJECT_ARRAY (object2)->real_length)
return FALSE;
diff --git a/hieroglyph/hgobject.c b/hieroglyph/hgobject.c
index e198e2b..3519a6f 100644
--- a/hieroglyph/hgobject.c
+++ b/hieroglyph/hgobject.c
@@ -234,6 +234,8 @@ hg_object_compare(hg_object_t *object1,
if (HG_OBJECT_GET_TYPE (object1) != HG_OBJECT_GET_TYPE (object2))
return FALSE;
+ if (object1 == object2)
+ return TRUE;
switch (HG_OBJECT_GET_TYPE (object1)) {
case HG_OBJECT_TYPE_NULL:
diff --git a/tests/hgarray.c b/tests/hgarray.c
index a79f3a1..d839481 100644
--- a/tests/hgarray.c
+++ b/tests/hgarray.c
@@ -25,6 +25,7 @@
#include "config.h"
#endif
#include <hieroglyph/hgarray.h>
+#include <hieroglyph/hgobject.h>
#include <hieroglyph/vm.h>
#include "main.h"
@@ -47,25 +48,147 @@ teardown(void)
/* array object */
TDEF (hg_object_array_new)
{
- g_print("FIXME: %s\n", __FUNCTION__);
+ obj = hg_object_array_new(vm, 10);
+
+ TNUL (obj);
+ fail_unless(HG_OBJECT_GET_N_OBJECTS (obj) == 1, "The amount of the object is different.");
+ fail_unless(HG_OBJECT_IS_ARRAY (obj), "Object isn't an array.");
+ fail_unless(HG_OBJECT_ARRAY (obj)->length == 10, "The amount of the array is different.");
+
+ hg_object_free(vm, obj);
+
+ obj = hg_object_array_new(vm, 0);
+
+ TNUL (obj);
+ fail_unless(HG_OBJECT_GET_N_OBJECTS (obj) == 1, "The amount of the object is different.");
+ fail_unless(HG_OBJECT_IS_ARRAY (obj), "Object isn't an array.");
+ fail_unless(HG_OBJECT_ARRAY (obj)->length == 0, "The amount of the array is different.");
+
+ hg_object_free(vm, obj);
}
TEND
TDEF (hg_object_array_subarray_new)
{
- g_print("FIXME: %s\n", __FUNCTION__);
+ gboolean flag;
+ hg_object_t *obj2;
+
+ obj = hg_object_array_new(vm, 10);
+
+ TNUL (obj);
+ fail_unless(HG_OBJECT_GET_N_OBJECTS (obj) == 1, "The amount of the object is different.");
+ fail_unless(HG_OBJECT_IS_ARRAY (obj), "Object isn't an array.");
+ fail_unless(HG_OBJECT_ARRAY (obj)->length == 10, "The amount of the array is different.");
+
+ obj2 = hg_object_array_subarray_new(vm, obj, 5, 5);
+ fail_unless(HG_OBJECT_GET_N_OBJECTS (obj2) == 1, "The amount of the object is different.");
+ fail_unless(HG_OBJECT_IS_ARRAY (obj2), "Object isn't an array.");
+ fail_unless(HG_OBJECT_ARRAY (obj2)->length == 0, "The amount of the array is different.");
+ fail_unless(HG_OBJECT_ARRAY (obj2)->real_length == 5, "The amount of the array is different.");
+
+ hg_object_free(vm, obj);
+ hg_object_free(vm, obj2);
+
+ obj = hg_object_new(vm, 1);
+
+ TNUL (obj);
+ fail_unless(HG_OBJECT_GET_N_OBJECTS (obj) == 1, "The amount of the object is different.");
+
+ if (hg_is_stacktrace_enabled)
+ flag = hg_is_stacktrace_enabled();
+ hg_use_stacktrace(FALSE);
+ obj2 = hg_object_array_subarray_new(vm, obj, 0, 0);
+ if (hg_is_stacktrace_enabled)
+ hg_use_stacktrace(flag);
+
+ fail_unless(obj2 == NULL, "Not allowed to create an array object without the parent array object.");
+
+ hg_object_free(vm, obj);
}
TEND
TDEF (hg_object_array_put)
{
- g_print("FIXME: %s\n", __FUNCTION__);
+ hg_object_t *obj2, *obj3;
+ gboolean retval;
+
+ obj = hg_object_array_new(vm, 5);
+
+ TNUL (obj);
+
+ obj2 = hg_object_boolean_new(vm, TRUE);
+
+ TNUL (obj2);
+ fail_unless(HG_OBJECT_IS_BOOLEAN (obj2), "Failed to create a boolean object");
+
+ retval = hg_object_array_put(vm, obj, obj2, 0);
+ fail_unless(retval == TRUE, "Failed to put an object to the array");
+ fail_unless(((hg_object_t **)HG_OBJECT_ARRAY_DATA (obj)->array)[0] == obj2, "Failed to put an object to the array expectedly");
+
+ hg_object_free(vm, obj);
+ hg_object_free(vm, obj2);
+
+ obj = hg_object_array_new(vm, 10);
+
+ TNUL (obj);
+
+ obj2 = hg_object_array_subarray_new(vm, obj, 5, 5);
+
+ TNUL (obj2);
+
+ obj3 = hg_object_boolean_new(vm, TRUE);
+
+ TNUL (obj3);
+
+ retval = hg_object_array_put(vm, obj2, obj3, 0);
+ fail_unless(retval == TRUE, "Failed to put an object to the sub array");
+ fail_unless(((hg_object_t **)HG_OBJECT_ARRAY_DATA (obj2)->array)[0] == obj3, "Failed to put an object to the sub array expectedly");
+ fail_unless(((hg_object_t **)HG_OBJECT_ARRAY_DATA (obj)->array)[5] == obj3, "Failed to sync between the array and the sub array on putting an object");
+
+ hg_object_free(vm, obj);
+ hg_object_free(vm, obj2);
+ hg_object_free(vm, obj3);
}
TEND
TDEF (hg_object_array_get)
{
- g_print("FIXME: %s\n", __FUNCTION__);
+ hg_object_t *obj2, *obj3;
+ gboolean retval;
+
+ obj = hg_object_array_new(vm, 10);
+ TNUL (obj);
+
+ obj2 = hg_object_boolean_new(vm, TRUE);
+ TNUL (obj2);
+
+ retval = hg_object_array_put(vm, obj, obj2, 0);
+ fail_unless(retval == TRUE, "Failed to put an object to the array");
+
+ obj3 = hg_object_array_get(vm, obj, 0);
+ fail_unless(obj3 != NULL, "Failed to get an object from the array");
+ fail_unless(hg_object_compare(obj2, obj3), "Failed to get an expected object from the array");
+
+ hg_object_free(vm, obj);
+ hg_object_free(vm, obj2);
+ hg_object_free(vm, obj3);
+
+ obj = hg_object_array_new(vm, 10);
+ TNUL (obj);
+
+ obj2 = hg_object_array_new(vm, 5);
+ TNUL (obj2);
+
+ retval = hg_object_array_put(vm, obj, obj2, 0);
+ fail_unless(retval == TRUE, "Failed to put an object to the array");
+
+ obj3 = hg_object_array_get(vm, obj, 0);
+ fail_unless(obj3 != NULL, "Failed to get an object from the array");
+ fail_unless(hg_object_compare(obj2, obj3), "Failed to get an expected object from the array");
+ fail_unless(obj2 == obj3, "Have to return the same object for the complex object");
+
+ hg_object_free(vm, obj);
+ hg_object_free(vm, obj2);
}
TEND