Browse Source

tests added

schaeferpp 3 years ago
parent
commit
6f5c683798
7 changed files with 229 additions and 2 deletions
  1. 2 2
      Makefile
  2. 2 0
      main.c
  3. 1 0
      minunit
  4. 23 0
      statement-expression
  5. 189 0
      test.c
  6. 10 0
      vehicle.c
  7. 2 0
      vehicle.h

+ 2 - 2
Makefile

@@ -1,7 +1,7 @@
 # CC=i686-w64-mingw32-gcc
 CC=gcc
-CFLAGS=-c -Wall -g $(shell pkg-config --cflags glib-2.0 gobject-2.0)
-LDFLAGS=$(shell pkg-config --libs glib-2.0 gobject-2.0) # -lpthread
+CFLAGS+=-c -Wall -g $(shell pkg-config --cflags glib-2.0 gobject-2.0)
+LDFLAGS+=$(shell pkg-config --libs glib-2.0 gobject-2.0) # -lpthread
 SOURCES=$(shell find . -name "*.c")
 OBJECTS=$(SOURCES:.c=.o)
 EXE=a.out

+ 2 - 0
main.c

@@ -15,6 +15,7 @@
 #include <glib-object.h>
 /* #include "vehicle.h" */
 
+#ifndef __TEST__
 int
 main(void)
 {
@@ -32,4 +33,5 @@ main(void)
     return 0;
 }
 
+#endif
 

+ 1 - 0
minunit

@@ -0,0 +1 @@
+Subproject commit 374af43b7a1c3e152d0f5d84f432694a99c2d950

+ 23 - 0
statement-expression

@@ -0,0 +1,23 @@
+/******************************************************************************
+ *
+ * file: abc.c
+ * date: 2016-06-29
+ * author: Paul Schaefer <schaefer.pp@gmail.com>
+ *
+ * Distributed under terms of the Apache 2.0 license.
+ *
+ * 
+ *
+ *****************************************************************************/
+
+#define ABC(A, B) ({A < B;})
+
+int
+main(void)
+{
+    if (ABC(4, 2))
+    {
+        printf("jo\n");
+    }
+}
+

+ 189 - 0
test.c

@@ -0,0 +1,189 @@
+/******************************************************************************
+ *
+ * file: test.c
+ * date: 2016-06-29
+ * author: Paul Schaefer <schaefer.pp@gmail.com>
+ *
+ * Distributed under terms of the Apache 2.0 license.
+ *
+ * 
+ *
+ *****************************************************************************/
+
+
+#include "object.h"
+#include "vehicle.h"
+#include "car.h"
+#include "minunit/minunit.h"
+#include <glib-object.h>
+#include <stdio.h>
+
+#ifdef __TEST__
+
+MU_TEST(test_vehicle_is_gobject)
+{
+    GObject *obj;
+
+    obj = g_object_new(APP_TYPE_VEHICLE, NULL);
+
+    mu_check(G_IS_OBJECT(obj));
+    g_object_unref(obj);
+}
+
+MU_TEST(test_car_is_gobject)
+{
+    GObject *obj;
+
+    obj = g_object_new(APP_TYPE_CAR, NULL);
+
+    mu_check(G_IS_OBJECT(obj));
+    g_object_unref(obj);
+}
+
+MU_TEST(test_car_is_vehicle)
+{
+    GObject *obj;
+
+    obj = g_object_new(APP_TYPE_CAR, NULL);
+
+    mu_check(APP_IS_VEHICLE(obj));
+    g_object_unref(obj);
+}
+
+MU_TEST(test_vehicle_is_no_car)
+{
+    GObject *obj;
+
+    obj = g_object_new(APP_TYPE_VEHICLE, NULL);
+
+    mu_check(!APP_IS_CAR(obj));
+    g_object_unref(obj);
+}
+
+MU_TEST(test_vehicle_max_speed)
+{
+    GObject *obj;
+
+    obj = g_object_new(APP_TYPE_VEHICLE, "max-speed", 170, NULL);
+
+    mu_check(app_vehicle_get_max_speed(APP_VEHICLE(obj)) == 170);
+}
+
+/* static void */
+/* test_max_speed(AppVehicle *v1, AppVehicle *v2, int(*check)(uint, uint)) */
+/* { */
+/*     GValue speed1 = G_VALUE_INIT, speed2 = G_VALUE_INIT; */
+
+/*     g_value_init(&speed1, G_TYPE_UINT); */
+/*     g_value_init(&speed2, G_TYPE_UINT); */
+
+/*     g_object_get_property(G_OBJECT(v1), "max-speed", &speed1); */
+/*     g_object_get_property(G_OBJECT(v2), "max-speed", &speed2); */
+
+/*     mu_check(check(app_vehicle_get_max_speed(v1),  app_vehicle_get_max_speed(v2))); */
+/*     mu_check(check(app_vehicle_get_max_speed(v1), g_value_get_uint(&speed2))); */
+/*     mu_check(check(g_value_get_uint(&speed1), app_vehicle_get_max_speed(v2))); */
+/*     mu_check(check(g_value_get_uint(&speed1), g_value_get_uint(&speed2))); */
+
+/*     g_value_unset(&speed1); */
+/*     g_value_unset(&speed2); */
+/* } */
+
+#define TEST_MAX_SPEED(v1, v2, OPERATOR) \
+    mu_check(app_vehicle_get_max_speed(v1) OPERATOR app_vehicle_get_max_speed(v2))
+
+#define TEST_PROP_VALS(obj1, obj2, propname, VALTYPE, GVALUEGETTER, OPERATOR) \
+{ \
+    int res = 0;\
+    GValue v1 = G_VALUE_INIT, v2 = G_VALUE_INIT; \
+    g_value_init(&v1, VALTYPE); \
+    g_value_init(&v2, VALTYPE); \
+    g_object_get_property(obj1, propname, &v1); \
+    g_object_get_property(obj2, propname, &v2); \
+    res = GVALUEGETTER(&v1) OPERATOR GVALUEGETTER(&v2); \
+    g_value_unset(&v1); g_value_unset(&v2); \
+    res; \
+}
+
+inline static int
+uint_eq(uint a, uint b)
+{
+    return a == b;
+}
+
+inline static int
+uint_lt(uint a, uint b)
+{
+    return a < b;
+}
+
+inline static int
+uint_gt(uint a, uint b)
+{
+    return a > b;
+}
+
+MU_TEST(test_two_vehicles_max_speed)
+{
+    GObject *obj1, *obj2;
+    GValue speed1 = G_VALUE_INIT, speed2 = G_VALUE_INIT;
+
+    g_value_init(&speed1, G_TYPE_UINT);
+    g_value_init(&speed2, G_TYPE_UINT);
+
+    obj1 = g_object_new(APP_TYPE_VEHICLE, "max-speed", 170, NULL);
+
+    obj2 = g_object_new(APP_TYPE_VEHICLE, NULL);
+
+    app_vehicle_set_max_speed(APP_VEHICLE(obj2), 170);
+
+    /* test_max_speed(APP_VEHICLE(obj1), APP_VEHICLE(obj2), uint_eq); */
+
+    g_value_set_uint(&speed1, 199);
+    g_value_set_uint(&speed2, 199);
+    g_object_set_property(obj1, "max-speed", &speed1);
+    g_object_set_property(obj2, "max-speed", &speed2);
+    /* 199 == 199 */
+    mu_check((TEST_PROP_VALS(obj1, obj2, "max-speed", G_TYPE_UINT, g_value_get_uint, ==)));
+
+
+    g_value_set_uint(&speed2, 99);
+    g_object_set_property(obj2, "max-speed", &speed2);
+    /* 199 > 99 */
+    mu_check((TEST_PROP_VALS(obj1, obj2, "max-speed", G_TYPE_UINT, g_value_get_uint, >)));
+
+    g_value_set_uint(&speed2, 999);
+    g_object_set_property(obj2, "max-speed", &speed2);
+    /* 199 < 999 */
+    mu_check((TEST_PROP_VALS(obj1, obj2, "max-speed", G_TYPE_UINT, g_value_get_uint, <)));
+
+
+    g_value_unset(&speed1);
+    g_value_unset(&speed2);
+}
+
+MU_TEST_SUITE(test_suite_inheritance)
+{
+    MU_RUN_TEST(test_vehicle_is_gobject);
+    MU_RUN_TEST(test_car_is_gobject);
+    MU_RUN_TEST(test_car_is_vehicle);
+    MU_RUN_TEST(test_vehicle_is_no_car);
+}
+
+MU_TEST_SUITE(test_suite_properties)
+{
+    MU_RUN_TEST(test_vehicle_max_speed);
+    MU_RUN_TEST(test_two_vehicles_max_speed);
+}
+
+int
+main(void)
+{
+    MU_RUN_SUITE(test_suite_inheritance);
+    MU_RUN_SUITE(test_suite_properties);
+    MU_REPORT();
+    return 0;
+}
+
+#endif
+

+ 10 - 0
vehicle.c

@@ -153,6 +153,16 @@ app_vehicle_new()
     return APP_VEHICLE(g_object_new(APP_TYPE_VEHICLE, NULL));
 }
 
+void
+app_vehicle_set_max_speed(AppVehicle *self, guint speed)
+{
+    AppVehiclePrivate *priv;
+
+    priv = app_vehicle_get_instance_private(self);
+
+    priv->max_speed = speed;
+}
+
 guint
 app_vehicle_get_max_speed(AppVehicle *self)
 {

+ 2 - 0
vehicle.h

@@ -41,6 +41,8 @@ void            app_vehicle_get_property     (GObject         *self,
 
 guint           app_vehicle_get_max_speed    (AppVehicle      *self);
 
+void            app_vehicle_set_max_speed    (AppVehicle      *self,
+                                              guint speed);
 
 G_END_DECLS