]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
tests: Add "pre-post-increment with side effect" tests to increase coverage
authorRico Tzschichholz <ricotz@ubuntu.com>
Thu, 13 Aug 2020 17:05:49 +0000 (19:05 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Thu, 13 Aug 2020 17:05:49 +0000 (19:05 +0200)
See https://gitlab.gnome.org/GNOME/vala/issues/1061

tests/Makefile.am
tests/control-flow/pre-post-increment-field.vala [new file with mode: 0644]
tests/control-flow/pre-post-increment-local.vala [new file with mode: 0644]
tests/control-flow/pre-post-increment-parameter.vala [new file with mode: 0644]
tests/control-flow/pre-post-increment-property.vala [new file with mode: 0644]

index c2505dcbace54aef3b9a9b009e4e662639997457..0f636c50c3a572a07230de5a31c5007d53d8a389 100644 (file)
@@ -222,6 +222,10 @@ TESTS = \
        control-flow/missing-return.test \
        control-flow/nested-conditional.vala \
        control-flow/pre-post-increment.vala \
+       control-flow/pre-post-increment-field.vala \
+       control-flow/pre-post-increment-local.vala \
+       control-flow/pre-post-increment-parameter.vala \
+       control-flow/pre-post-increment-property.vala \
        control-flow/switch.vala \
        control-flow/switch-enum.vala \
        control-flow/sideeffects.vala \
diff --git a/tests/control-flow/pre-post-increment-field.vala b/tests/control-flow/pre-post-increment-field.vala
new file mode 100644 (file)
index 0000000..cf742a3
--- /dev/null
@@ -0,0 +1,75 @@
+int field;
+
+void main () {
+       // incrementing
+       {
+               field = 1;
+               int res = field + field++;
+               assert (res == 2);
+               assert (field == 2);
+       }
+       {
+               field = 1;
+               int res = field++ + field;
+               assert (res == 3);
+               assert (field == 2);
+       }
+       {
+               field = 1;
+               int res = field + ++field;
+               assert (res == 3);
+               assert (field == 2);
+       }
+       {
+               field = 1;
+               int res = ++field + field;
+               assert (res == 4);
+               assert (field == 2);
+       }
+       {
+               field = 1;
+               assert (field++ == 1);
+               assert (field == 2);
+       }
+       {
+               field = 1;
+               assert (++field == 2);
+               assert (field == 2);
+       }
+
+       // decrementing
+       {
+               field = 1;
+               int d = field + field--;
+               assert (d == 2);
+               assert (field == 0);
+       }
+       {
+               field = 1;
+               int res = field-- + field;
+               assert (res == 1);
+               assert (field == 0);
+       }
+       {
+               field = 1;
+               int res = field + --field;
+               assert (res == 1);
+               assert (field == 0);
+       }
+       {
+               field = 1;
+               int res = --field + field;
+               assert (res == 0);
+               assert (field == 0);
+       }
+       {
+               field = 1;
+               assert (field-- == 1);
+               assert (field == 0);
+       }
+       {
+               field = 1;
+               assert (--field == 0);
+               assert (field == 0);
+       }
+}
diff --git a/tests/control-flow/pre-post-increment-local.vala b/tests/control-flow/pre-post-increment-local.vala
new file mode 100644 (file)
index 0000000..d630871
--- /dev/null
@@ -0,0 +1,73 @@
+void main () {
+       // incrementing
+       {
+               int local = 1;
+               int res = local + local++;
+               assert (res == 2);
+               assert (local == 2);
+       }
+       {
+               int local = 1;
+               int res = local++ + local;
+               assert (res == 3);
+               assert (local == 2);
+       }
+       {
+               int local = 1;
+               int res = local + ++local;
+               assert (res == 3);
+               assert (local == 2);
+       }
+       {
+               int local = 1;
+               int res = ++local + local;
+               assert (res == 4);
+               assert (local == 2);
+       }
+       {
+               int local = 1;
+               assert (local++ == 1);
+               assert (local == 2);
+       }
+       {
+               int local = 1;
+               assert (++local == 2);
+               assert (local == 2);
+       }
+
+       // decrementing
+       {
+               int local = 1;
+               int res = local + local--;
+               assert (res == 2);
+               assert (local == 0);
+       }
+       {
+               int local = 1;
+               int res = local-- + local;
+               assert (res == 1);
+               assert (local == 0);
+       }
+       {
+               int local = 1;
+               int res = local + --local;
+               assert (res == 1);
+               assert (local == 0);
+       }
+       {
+               int local = 1;
+               int res = --local + local;
+               assert (res == 0);
+               assert (local == 0);
+       }
+       {
+               int local = 1;
+               assert (local-- == 1);
+               assert (local == 0);
+       }
+       {
+               int local = 1;
+               assert (--local == 0);
+               assert (local == 0);
+       }
+}
diff --git a/tests/control-flow/pre-post-increment-parameter.vala b/tests/control-flow/pre-post-increment-parameter.vala
new file mode 100644 (file)
index 0000000..0cd4101
--- /dev/null
@@ -0,0 +1,77 @@
+void test_parameter (int parameter) {
+       // incrementing
+       {
+               parameter = 1;
+               int res = parameter + parameter++;
+               assert (res == 2);
+               assert (parameter == 2);
+       }
+       {
+               parameter = 1;
+               int res = parameter++ + parameter;
+               assert (res == 3);
+               assert (parameter == 2);
+       }
+       {
+               parameter = 1;
+               int res = parameter + ++parameter;
+               assert (res == 3);
+               assert (parameter == 2);
+       }
+       {
+               parameter = 1;
+               int res = ++parameter + parameter;
+               assert (res == 4);
+               assert (parameter == 2);
+       }
+       {
+               parameter = 1;
+               assert (parameter++ == 1);
+               assert (parameter == 2);
+       }
+       {
+               parameter = 1;
+               assert (++parameter == 2);
+               assert (parameter == 2);
+       }
+
+       // decrementing
+       {
+               parameter = 1;
+               int res = parameter + parameter--;
+               assert (res == 2);
+               assert (parameter == 0);
+       }
+       {
+               parameter = 1;
+               int res = parameter-- + parameter;
+               assert (res == 1);
+               assert (parameter == 0);
+       }
+       {
+               parameter = 1;
+               int res = parameter + --parameter;
+               assert (res == 1);
+               assert (parameter == 0);
+       }
+       {
+               parameter = 1;
+               int res = --parameter + parameter;
+               assert (res == 0);
+               assert (parameter == 0);
+       }
+       {
+               parameter = 1;
+               assert (parameter-- == 1);
+               assert (parameter == 0);
+       }
+       {
+               parameter = 1;
+               assert (--parameter == 0);
+               assert (parameter == 0);
+       }
+}
+
+void main () {
+       test_parameter (1);
+}
diff --git a/tests/control-flow/pre-post-increment-property.vala b/tests/control-flow/pre-post-increment-property.vala
new file mode 100644 (file)
index 0000000..148506c
--- /dev/null
@@ -0,0 +1,152 @@
+class Foo {
+       public int property { get; set; }
+
+       public Foo () {
+               // incrementing
+               {
+                       property = 1;
+                       int res = property + property++;
+                       assert (res == 2);
+                       assert (property == 2);
+               }
+               {
+                       property = 1;
+                       int res = property++ + property;
+                       assert (res == 3);
+                       assert (property == 2);
+               }
+               {
+                       property = 1;
+                       int res = property + ++property;
+                       assert (res == 3);
+                       assert (property == 2);
+               }
+               {
+                       property = 1;
+                       int res = ++property + property;
+                       assert (res == 4);
+                       assert (property == 2);
+               }
+               {
+                       property = 1;
+                       assert (property++ == 1);
+                       assert (property == 2);
+               }
+               {
+                       property = 1;
+                       assert (++property == 2);
+                       assert (property == 2);
+               }
+
+               // decrementing
+               {
+                       property = 1;
+                       int res = property + property--;
+                       assert (res == 2);
+                       assert (property == 0);
+               }
+               {
+                       property = 1;
+                       int res = property-- + property;
+                       assert (res == 1);
+                       assert (property == 0);
+               }
+               {
+                       property = 1;
+                       int res = property + --property;
+                       assert (res == 1);
+                       assert (property == 0);
+               }
+               {
+                       property = 1;
+                       int res = --property + property;
+                       assert (res == 0);
+                       assert (property == 0);
+               }
+               {
+                       property = 1;
+                       assert (property-- == 1);
+                       assert (property == 0);
+               }
+               {
+                       property = 1;
+                       assert (--property == 0);
+                       assert (property == 0);
+               }
+       }
+}
+
+void main () {
+       var foo = new Foo ();
+       // incrementing
+       {
+               foo.property = 1;
+               int res = foo.property + foo.property++;
+               assert (res == 2);
+               assert (foo.property == 2);
+       }
+       {
+               foo.property = 1;
+               int res = foo.property++ + foo.property;
+               assert (res == 3);
+               assert (foo.property == 2);
+       }
+       {
+               foo.property = 1;
+               int res = foo.property + ++foo.property;
+               assert (res == 3);
+               assert (foo.property == 2);
+       }
+       {
+               foo.property = 1;
+               int res = ++foo.property + foo.property;
+               assert (res == 4);
+               assert (foo.property == 2);
+       }
+       {
+               foo.property = 1;
+               assert (foo.property++ == 1);
+               assert (foo.property == 2);
+       }
+       {
+               foo.property = 1;
+               assert (++foo.property == 2);
+               assert (foo.property == 2);
+       }
+
+       // decrementing
+       {
+               foo.property = 1;
+               int res = foo.property + foo.property--;
+               assert (res == 2);
+               assert (foo.property == 0);
+       }
+       {
+               foo.property = 1;
+               int res = foo.property-- + foo.property;
+               assert (res == 1);
+               assert (foo.property == 0);
+       }
+       {
+               foo.property = 1;
+               int res = foo.property + --foo.property;
+               assert (res == 1);
+               assert (foo.property == 0);
+       }
+       {
+               foo.property = 1;
+               int res = --foo.property + foo.property;
+               assert (res == 0);
+               assert (foo.property == 0);
+       }
+       {
+               foo.property = 1;
+               assert (foo.property-- == 1);
+               assert (foo.property == 0);
+       }
+       {
+               foo.property = 1;
+               assert (--foo.property == 0);
+               assert (foo.property == 0);
+       }
+}