summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVladyslav Stovmanenko <flaviusglamfenix@gmail.com>2022-05-06 14:02:05 +0000
committerRico Tzschichholz <ricotz@ubuntu.com>2022-08-15 18:53:20 +0000
commitd8c0a198a7cbdaaceab8adf21316f4e1930ab79f (patch)
tree6113e1356e62a3b608d8fd5e856d7c124b6e8636
parente6547fd3248cca12f57915623a1f69a4ad437cc9 (diff)
downloadvala-d8c0a198a7cbdaaceab8adf21316f4e1930ab79f.tar.gz
Update tests
-rw-r--r--tests/Makefile.am1
-rw-r--r--tests/basic-types/arrays-move-with-class.vala30
-rw-r--r--tests/basic-types/arrays.c-expected152
-rw-r--r--tests/basic-types/arrays.vala28
-rw-r--r--tests/posix/arrays.c-expected129
5 files changed, 330 insertions, 10 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 147c66676..8bc5a3e11 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -56,6 +56,7 @@ TESTS = \
basic-types/arrays.vala \
basic-types/arrays-generics.vala \
basic-types/arrays-fixed-assignment.vala \
+ basic-types/arrays-move-with-class.vala \
basic-types/array-uint8-uchar-compat.vala \
basic-types/pointers.vala \
basic-types/pointers-arithmetic.vala \
diff --git a/tests/basic-types/arrays-move-with-class.vala b/tests/basic-types/arrays-move-with-class.vala
new file mode 100644
index 000000000..4a6525703
--- /dev/null
+++ b/tests/basic-types/arrays-move-with-class.vala
@@ -0,0 +1,30 @@
+class TestDestructorCalls {
+ public static int destructor_calls = 0;
+ private int idx;
+
+ public TestDestructorCalls (int idx) { this.idx = idx; }
+
+ ~TestDestructorCalls () { destructor_calls++; }
+}
+
+void test_array_with_classes_move (int src, int dest, int count, int expected_destructor_calls)
+{
+ const int arr_size = 5;
+ TestDestructorCalls.destructor_calls = 0;
+ TestDestructorCalls[] arr = new TestDestructorCalls[arr_size];
+ for(int i=0; i<arr_size; i++)
+ {
+ arr[i] = new TestDestructorCalls (i);
+ }
+
+ arr.move (src, dest, count);
+ assert (TestDestructorCalls.destructor_calls == expected_destructor_calls);
+}
+
+void main()
+{
+ test_array_with_classes_move(0, 2, 3, 1);
+ test_array_with_classes_move(2, 0, 3, 2);
+ test_array_with_classes_move(0, 3, 1, 1);
+
+}
diff --git a/tests/basic-types/arrays.c-expected b/tests/basic-types/arrays.c-expected
index 5c4a16709..7839707a6 100644
--- a/tests/basic-types/arrays.c-expected
+++ b/tests/basic-types/arrays.c-expected
@@ -20,6 +20,9 @@
#define _g_free0(var) ((var == NULL) ? NULL : (var = (g_free (var), NULL)))
typedef gint (*SimpleFunc) (void);
+#define TYPE_TEST_MOVE_CALL (test_move_call_get_type ())
+typedef struct _TestMoveCall TestMoveCall;
+
#define TYPE_FOO (foo_get_type ())
typedef struct _Foo Foo;
@@ -34,6 +37,10 @@ typedef struct _Manam Manam;
#define _vala_return_val_if_fail(expr, msg, val) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return val; }
#define _vala_warn_if_fail(expr, msg) if G_LIKELY (expr) ; else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);
+struct _TestMoveCall {
+ gint idx;
+};
+
struct _Foo {
const gchar* array[2];
gint bar;
@@ -136,7 +143,16 @@ static gboolean _vala_valavoid_array_contains (void* * stack,
VALA_EXTERN void test_explicit_copying (void);
static gint* _vala_array_dup11 (gint* self,
gssize length);
-VALA_EXTERN void test_array_move (void);
+VALA_EXTERN void test_array_with_primitives_move (void);
+VALA_EXTERN GType test_move_call_get_type (void) G_GNUC_CONST ;
+VALA_EXTERN TestMoveCall* test_move_call_dup (const TestMoveCall* self);
+VALA_EXTERN void test_move_call_free (TestMoveCall* self);
+VALA_EXTERN void test_move_call_init (TestMoveCall *self,
+ gint idx);
+VALA_EXTERN void test_array_with_struct_move (gint src,
+ gint dest,
+ gint count,
+ gint expected_destructor_calls);
VALA_EXTERN void test_array_resize (void);
VALA_EXTERN GType foo_get_type (void) G_GNUC_CONST ;
VALA_EXTERN Foo* foo_dup (const Foo* self);
@@ -166,6 +182,7 @@ static void _vala_array_free (gpointer array,
GDestroyNotify destroy_func);
static void _vala_array_move (gpointer array,
gsize element_size,
+ GDestroyNotify free_func,
gssize src,
gssize dest,
gssize length);
@@ -1300,7 +1317,7 @@ test_explicit_copying (void)
}
void
-test_array_move (void)
+test_array_with_primitives_move (void)
{
gint* a = NULL;
gint* _tmp0_;
@@ -1308,6 +1325,7 @@ test_array_move (void)
gint _a_size_;
gint _tmp1_;
gint _tmp2_;
+ gint _tmp3_;
_tmp0_ = g_new0 (gint, 9);
_tmp0_[0] = 1;
_tmp0_[1] = 2;
@@ -1323,13 +1341,120 @@ test_array_move (void)
_a_size_ = a_length1;
_tmp1_ = a[4];
_vala_assert (_tmp1_ == 5, "a[4] == 5");
- _vala_array_move (a, sizeof (gint), 4, 0, 5);
+ _vala_array_move (a, sizeof (gint), NULL, 4, 0, 5);
_tmp2_ = a[4];
_vala_assert (_tmp2_ == 9, "a[4] == 9");
+ _tmp3_ = a[8];
+ _vala_assert (_tmp3_ == 0, "a[8] == 0");
a = (g_free (a), NULL);
}
void
+test_move_call_init (TestMoveCall *self,
+ gint idx)
+{
+ memset (self, 0, sizeof (TestMoveCall));
+ (*self).idx = idx;
+}
+
+TestMoveCall*
+test_move_call_dup (const TestMoveCall* self)
+{
+ TestMoveCall* dup;
+ dup = g_new0 (TestMoveCall, 1);
+ memcpy (dup, self, sizeof (TestMoveCall));
+ return dup;
+}
+
+void
+test_move_call_free (TestMoveCall* self)
+{
+ g_free (self);
+}
+
+static GType
+test_move_call_get_type_once (void)
+{
+ GType test_move_call_type_id;
+ test_move_call_type_id = g_boxed_type_register_static ("TestMoveCall", (GBoxedCopyFunc) test_move_call_dup, (GBoxedFreeFunc) test_move_call_free);
+ return test_move_call_type_id;
+}
+
+GType
+test_move_call_get_type (void)
+{
+ static volatile gsize test_move_call_type_id__once = 0;
+ if (g_once_init_enter (&test_move_call_type_id__once)) {
+ GType test_move_call_type_id;
+ test_move_call_type_id = test_move_call_get_type_once ();
+ g_once_init_leave (&test_move_call_type_id__once, test_move_call_type_id);
+ }
+ return test_move_call_type_id__once;
+}
+
+void
+test_array_with_struct_move (gint src,
+ gint dest,
+ gint count,
+ gint expected_destructor_calls)
+{
+ static const gint arr_size = 5;
+ TestMoveCall* arr = NULL;
+ TestMoveCall* _tmp0_;
+ gint arr_length1;
+ gint _arr_size_;
+ TestMoveCall testObj = {0};
+ TestMoveCall* _tmp5_;
+ gint _tmp5__length1;
+ TestMoveCall _tmp6_;
+ TestMoveCall* _tmp7_;
+ gint _tmp7__length1;
+ TestMoveCall _tmp8_;
+ TestMoveCall _tmp9_;
+ _tmp0_ = g_new0 (TestMoveCall, arr_size);
+ arr = _tmp0_;
+ arr_length1 = arr_size;
+ _arr_size_ = arr_length1;
+ {
+ gint i = 0;
+ i = 0;
+ {
+ gboolean _tmp1_ = FALSE;
+ _tmp1_ = TRUE;
+ while (TRUE) {
+ TestMoveCall* _tmp3_;
+ gint _tmp3__length1;
+ TestMoveCall _tmp4_ = {0};
+ if (!_tmp1_) {
+ gint _tmp2_;
+ _tmp2_ = i;
+ i = _tmp2_ + 1;
+ }
+ _tmp1_ = FALSE;
+ if (!(i < arr_size)) {
+ break;
+ }
+ _tmp3_ = arr;
+ _tmp3__length1 = arr_length1;
+ test_move_call_init (&_tmp4_, i);
+ _tmp3_[i] = _tmp4_;
+ }
+ }
+ }
+ _tmp5_ = arr;
+ _tmp5__length1 = arr_length1;
+ _tmp6_ = _tmp5_[src];
+ testObj = _tmp6_;
+ _vala_array_move (arr, sizeof (TestMoveCall), NULL, src, dest, count);
+ _tmp7_ = arr;
+ _tmp7__length1 = arr_length1;
+ _tmp8_ = _tmp7_[dest];
+ _tmp9_ = testObj;
+ _vala_assert (_tmp8_.idx == _tmp9_.idx, "arr[dest].idx == testObj.idx");
+ arr = (g_free (arr), NULL);
+}
+
+void
test_array_resize (void)
{
gint* a = NULL;
@@ -1637,7 +1762,7 @@ _vala_main (void)
test_delegate_array ();
test_void_array ();
test_explicit_copying ();
- test_array_move ();
+ test_array_with_primitives_move ();
test_array_resize ();
test_struct_array ();
test_fixed_array ();
@@ -1678,10 +1803,29 @@ _vala_array_free (gpointer array,
static void
_vala_array_move (gpointer array,
gsize element_size,
+ GDestroyNotify free_func,
gssize src,
gssize dest,
gssize length)
{
+ if (free_func != NULL) {
+ if ((src < dest) && ((src + length) > dest)) {
+ gssize i;
+ for (i = dest; i < (src + length); i++) {
+ free_func (((void**) array)[i]);
+ }
+ } else if ((src > dest) && (src < (dest + length))) {
+ gssize i;
+ for (i = dest; i < src; i++) {
+ free_func (((void**) array)[i]);
+ }
+ } else if (src != dest) {
+ gssize i;
+ for (i = dest; i < (dest + length); i++) {
+ free_func (((void**) array)[i]);
+ }
+ }
+ }
memmove (((char*) array) + (dest * element_size), ((char*) array) + (src * element_size), length * element_size);
if ((src < dest) && ((src + length) > dest)) {
memset (((char*) array) + (src * element_size), 0, (dest - src) * element_size);
diff --git a/tests/basic-types/arrays.vala b/tests/basic-types/arrays.vala
index 1321c1b31..0a94a6acb 100644
--- a/tests/basic-types/arrays.vala
+++ b/tests/basic-types/arrays.vala
@@ -213,11 +213,32 @@ void test_explicit_copying () {
assert (a0[1] == a1[1]);
}
-void test_array_move () {
+void test_array_with_primitives_move () {
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
assert (a[4] == 5);
a.move (4, 0, 5);
assert (a[4] == 9);
+ assert (a[8] == 0);
+}
+
+struct TestMoveCall {
+ int idx;
+
+ public TestMoveCall (int idx) { this.idx = idx; }
+}
+
+void test_array_with_struct_move (int src, int dest, int count, int expected_destructor_calls)
+{
+ const int arr_size = 5;
+ TestMoveCall[] arr = new TestMoveCall[arr_size];
+ for(int i=0; i<arr_size; i++)
+ {
+ arr[i] = TestMoveCall (i);
+ }
+
+ TestMoveCall testObj = arr[src];
+ arr.move (src, dest, count);
+ assert(arr[dest].idx == testObj.idx);
}
void test_array_resize () {
@@ -300,7 +321,10 @@ void main () {
test_delegate_array ();
test_void_array ();
test_explicit_copying ();
- test_array_move ();
+ test_array_with_primitives_move ();
+ //test_array_with_struct_move(0, 2, 3, 1);
+ //test_array_with_struct_move(2, 0, 3, 2);
+ //test_array_with_struct_move(0, 3, 1, 1);
test_array_resize ();
test_struct_array ();
test_fixed_array ();
diff --git a/tests/posix/arrays.c-expected b/tests/posix/arrays.c-expected
index b59712f6b..47710c9cf 100644
--- a/tests/posix/arrays.c-expected
+++ b/tests/posix/arrays.c-expected
@@ -22,12 +22,17 @@
#define _free0(var) ((var == NULL) ? NULL : (var = (free (var), NULL)))
typedef void (*ValaDestroyNotify) (void* data);
typedef int (*SimpleFunc) (void);
+typedef struct _TestMoveCall TestMoveCall;
typedef struct _Foo Foo;
typedef struct _Bar Bar;
typedef struct _Manam Manam;
#define _manam_free0(var) ((var == NULL) ? NULL : (var = (manam_free (var), NULL)))
#define VALA_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
+struct _TestMoveCall {
+ int idx;
+};
+
struct _Foo {
const char* array[2];
int bar;
@@ -130,7 +135,15 @@ static bool _vala_valavoid_array_contains (void* * stack,
VALA_EXTERN void test_explicit_copying (void);
static int* _vala_array_dup11 (int* self,
ssize_t length);
-VALA_EXTERN void test_array_move (void);
+VALA_EXTERN void test_array_with_primitives_move (void);
+VALA_EXTERN TestMoveCall* test_move_call_dup (const TestMoveCall* self);
+VALA_EXTERN void test_move_call_free (TestMoveCall* self);
+VALA_EXTERN void test_move_call_init (TestMoveCall *self,
+ int idx);
+VALA_EXTERN void test_array_with_struct_move (int src,
+ int dest,
+ int count,
+ int expected_destructor_calls);
VALA_EXTERN void test_array_resize (void);
VALA_EXTERN Foo* foo_dup (const Foo* self);
VALA_EXTERN void foo_free (Foo* self);
@@ -157,6 +170,7 @@ static void _vala_array_free (void* array,
ValaDestroyNotify destroy_func);
static void _vala_array_move (void* array,
size_t element_size,
+ ValaDestroyNotify free_func,
ssize_t src,
ssize_t dest,
ssize_t length);
@@ -1338,7 +1352,7 @@ test_explicit_copying (void)
}
void
-test_array_move (void)
+test_array_with_primitives_move (void)
{
int* a = NULL;
int* _tmp0_;
@@ -1346,6 +1360,7 @@ test_array_move (void)
int _a_size_;
int _tmp1_;
int _tmp2_;
+ int _tmp3_;
_tmp0_ = calloc (9, sizeof (int));
_tmp0_[0] = 1;
_tmp0_[1] = 2;
@@ -1361,13 +1376,100 @@ test_array_move (void)
_a_size_ = a_length1;
_tmp1_ = a[4];
assert (_tmp1_ == 5);
- _vala_array_move (a, sizeof (int), 4, 0, 5);
+ _vala_array_move (a, sizeof (int), NULL, 4, 0, 5);
_tmp2_ = a[4];
assert (_tmp2_ == 9);
+ _tmp3_ = a[8];
+ assert (_tmp3_ == 0);
a = (free (a), NULL);
}
void
+test_move_call_init (TestMoveCall *self,
+ int idx)
+{
+ memset (self, 0, sizeof (TestMoveCall));
+ (*self).idx = idx;
+}
+
+TestMoveCall*
+test_move_call_dup (const TestMoveCall* self)
+{
+ TestMoveCall* dup;
+ dup = calloc (1, sizeof (TestMoveCall));
+ memcpy (dup, self, sizeof (TestMoveCall));
+ return dup;
+}
+
+void
+test_move_call_free (TestMoveCall* self)
+{
+ free (self);
+}
+
+void
+test_array_with_struct_move (int src,
+ int dest,
+ int count,
+ int expected_destructor_calls)
+{
+ static const int arr_size = 5;
+ TestMoveCall* arr = NULL;
+ TestMoveCall* _tmp0_;
+ int arr_length1;
+ int _arr_size_;
+ TestMoveCall testObj = {0};
+ TestMoveCall* _tmp5_;
+ int _tmp5__length1;
+ TestMoveCall _tmp6_;
+ TestMoveCall* _tmp7_;
+ int _tmp7__length1;
+ TestMoveCall _tmp8_;
+ TestMoveCall _tmp9_;
+ _tmp0_ = calloc (arr_size, sizeof (TestMoveCall));
+ arr = _tmp0_;
+ arr_length1 = arr_size;
+ _arr_size_ = arr_length1;
+ {
+ int i = 0;
+ i = 0;
+ {
+ bool _tmp1_ = false;
+ _tmp1_ = true;
+ while (true) {
+ TestMoveCall* _tmp3_;
+ int _tmp3__length1;
+ TestMoveCall _tmp4_ = {0};
+ if (!_tmp1_) {
+ int _tmp2_;
+ _tmp2_ = i;
+ i = _tmp2_ + 1;
+ }
+ _tmp1_ = false;
+ if (!(i < arr_size)) {
+ break;
+ }
+ _tmp3_ = arr;
+ _tmp3__length1 = arr_length1;
+ test_move_call_init (&_tmp4_, i);
+ _tmp3_[i] = _tmp4_;
+ }
+ }
+ }
+ _tmp5_ = arr;
+ _tmp5__length1 = arr_length1;
+ _tmp6_ = _tmp5_[src];
+ testObj = _tmp6_;
+ _vala_array_move (arr, sizeof (TestMoveCall), NULL, src, dest, count);
+ _tmp7_ = arr;
+ _tmp7__length1 = arr_length1;
+ _tmp8_ = _tmp7_[dest];
+ _tmp9_ = testObj;
+ assert (_tmp8_.idx == _tmp9_.idx);
+ arr = (free (arr), NULL);
+}
+
+void
test_array_resize (void)
{
int* a = NULL;
@@ -1615,7 +1717,7 @@ _vala_main (void)
test_delegate_array ();
test_void_array ();
test_explicit_copying ();
- test_array_move ();
+ test_array_with_primitives_move ();
test_array_resize ();
test_struct_array ();
test_fixed_array ();
@@ -1656,10 +1758,29 @@ _vala_array_free (void* array,
static void
_vala_array_move (void* array,
size_t element_size,
+ ValaDestroyNotify free_func,
ssize_t src,
ssize_t dest,
ssize_t length)
{
+ if (free_func != NULL) {
+ if ((src < dest) && ((src + length) > dest)) {
+ ssize_t i;
+ for (i = dest; i < (src + length); i++) {
+ free_func (((void**) array)[i]);
+ }
+ } else if ((src > dest) && (src < (dest + length))) {
+ ssize_t i;
+ for (i = dest; i < src; i++) {
+ free_func (((void**) array)[i]);
+ }
+ } else if (src != dest) {
+ ssize_t i;
+ for (i = dest; i < (dest + length); i++) {
+ free_func (((void**) array)[i]);
+ }
+ }
+ }
memmove (((char*) array) + (dest * element_size), ((char*) array) + (src * element_size), length * element_size);
if ((src < dest) && ((src + length) > dest)) {
memset (((char*) array) + (src * element_size), 0, (dest - src) * element_size);