poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/2] testsuite: Add more tests for libpoke API


From: Mohammad-Reza Nabipoor
Subject: [PATCH 2/2] testsuite: Add more tests for libpoke API
Date: Fri, 20 Nov 2020 19:21:58 +0330

2020-11-20  Mohammad-Reza Nabipoor  <m.nabipoor@yahoo.com>

        * libpoke/libpoke.c (pk_compiler_new): Make sure all feilds of
        `term_if` are non-NULL.
        * libpoke/pk-val.c (pk_make_offset): Reject zero value for units.
        * testsuite/poke.libpoke/values.c (test_simple_values): Add more
        tests to cover all simple data types: signed/unsigned ints, strings
        and offsets.
        * testsuite/poke.libpoke/api.c: New test file to unit test libpoke
        public API.
        * testsuite/poke.libpoke/Makefile.am: Add build rules for `api` test.
        * testsuite/poke.libpoke/libpoke.exp: Add support for new `api`
        executable.
---
 ChangeLog                          |  14 ++++
 libpoke/libpoke.c                  |  12 ++-
 libpoke/pk-val.c                   |   1 +
 testsuite/poke.libpoke/Makefile.am |  14 +++-
 testsuite/poke.libpoke/api.c       | 109 +++++++++++++++++++++++++
 testsuite/poke.libpoke/libpoke.exp |   3 +
 testsuite/poke.libpoke/values.c    | 123 +++++++++++++++++++++++------
 7 files changed, 250 insertions(+), 26 deletions(-)
 create mode 100644 testsuite/poke.libpoke/api.c

diff --git a/ChangeLog b/ChangeLog
index 15decd65..5e524891 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2020-11-20  Mohammad-Reza Nabipoor  <m.nabipoor@yahoo.com>
+
+       * libpoke/libpoke.c (pk_compiler_new): Make sure all feilds of
+       `term_if` are non-NULL.
+       * libpoke/pk-val.c (pk_make_offset): Reject zero value for units.
+       * testsuite/poke.libpoke/values.c (test_simple_values): Add more
+       tests to cover all simple data types: signed/unsigned ints, strings
+       and offsets.
+       * testsuite/poke.libpoke/api.c: New test file to unit test libpoke
+       public API.
+       * testsuite/poke.libpoke/Makefile.am: Add build rules for `api` test.
+       * testsuite/poke.libpoke/libpoke.exp: Add support for new `api`
+       executable.
+
 2020-11-20  Mohammad-Reza Nabipoor  <m.nabipoor@yahoo.com>
 
        * bootstrap.conf (gnu_modules): Add module `read-file`.
diff --git a/libpoke/libpoke.c b/libpoke/libpoke.c
index 1db3abbd..e452d39b 100644
--- a/libpoke/libpoke.c
+++ b/libpoke/libpoke.c
@@ -46,9 +46,17 @@ __attribute__ ((visibility ("hidden")));
 pk_compiler
 pk_compiler_new (struct pk_term_if *term_if)
 {
-  pk_compiler pkc
-    = malloc (sizeof (struct pk_compiler));
+  pk_compiler pkc;
 
+  if (!term_if)
+    return NULL;
+
+  if (!term_if->flush_fn || !term_if->puts_fn || !term_if->printf_fn
+      || !term_if->indent_fn || !term_if->class_fn || !term_if->end_class_fn
+      || !term_if->hyperlink_fn || !term_if->end_hyperlink_fn)
+    return NULL;
+
+  pkc = malloc (sizeof (struct pk_compiler));
   if (pkc)
     {
       /* Determine the path to the compiler's runtime files.  */
diff --git a/libpoke/pk-val.c b/libpoke/pk-val.c
index db441e3c..f36e645f 100644
--- a/libpoke/pk-val.c
+++ b/libpoke/pk-val.c
@@ -111,6 +111,7 @@ pk_make_offset (pk_val magnitude, pk_val unit)
 {
   if (!PVM_IS_INTEGRAL (magnitude)
       || !PVM_IS_ULONG (unit)
+      || PVM_VAL_ULONG (unit) == 0
       || PVM_VAL_ULONG_SIZE (unit) != 64)
     return PK_NULL;
   else
diff --git a/testsuite/poke.libpoke/Makefile.am 
b/testsuite/poke.libpoke/Makefile.am
index c2d4141f..d9d98c26 100644
--- a/testsuite/poke.libpoke/Makefile.am
+++ b/testsuite/poke.libpoke/Makefile.am
@@ -18,7 +18,7 @@ EXTRA_DIST = \
 
 COMMON = term-if.h
 
-check_PROGRAMS = values
+check_PROGRAMS = values api
 values_SOURCES = $(COMMON) values.c
 
 values_CPPFLAGS = -I$(top_builddir)/gl -I$(top_srcdir)/gl \
@@ -30,3 +30,15 @@ values_CPPFLAGS = -I$(top_builddir)/gl -I$(top_srcdir)/gl \
 values_LDADD = $(top_builddir)/gl/libgnu.la \
                $(top_builddir)/libpoke/libpoke.la \
                $(LTLIBTEXTSTYLE)
+
+api_SOURCES = $(COMMON) api.c
+
+api_CPPFLAGS = -I$(top_builddir)/gl -I$(top_srcdir)/gl \
+                  -I$(top_srcdir)/common \
+                  -DTESTDIR=\"$(abs_srcdir)\" \
+                  -DPKGDATADIR=\"$(pkgdatadir)\" \
+                  -I$(top_srcdir)/libpoke -I$(top_builddir)/libpoke
+
+api_LDADD = $(top_builddir)/gl/libgnu.la \
+               $(top_builddir)/libpoke/libpoke.la \
+               $(LTLIBTEXTSTYLE)
diff --git a/testsuite/poke.libpoke/api.c b/testsuite/poke.libpoke/api.c
new file mode 100644
index 00000000..b7c3be45
--- /dev/null
+++ b/testsuite/poke.libpoke/api.c
@@ -0,0 +1,109 @@
+/* api.c -- Unit tests for the libpoke public API */
+
+/* Copyright (C) 2020 The poke authors */
+
+/* This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <dejagnu.h>
+#include <err.h>
+#include "read-file.h"
+#include "libpoke.h"
+
+#include "term-if.h"
+
+#define T(name, cond)                                                         \
+  do                                                                          \
+    {                                                                         \
+      if (cond)                                                               \
+        pass (name);                                                          \
+      else                                                                    \
+        fail (name);                                                          \
+    }                                                                         \
+  while (0)
+
+static pk_compiler
+test_pk_compiler_new (void)
+{
+  pk_compiler pkc;
+  struct pk_term_if tif;
+
+  pkc = pk_compiler_new (NULL);
+  T ("pk_compiler_new_1", pkc == NULL);
+
+#define TT(n)                                                                 \
+  do                                                                          \
+    {                                                                         \
+      pkc = pk_compiler_new (&tif);                                           \
+      T ("pk_compiler_new_" #n, pkc == NULL);                                 \
+    }                                                                         \
+  while (0)
+
+  memset (&tif, 0, sizeof (struct pk_term_if));
+  TT (2);
+
+  tif.flush_fn = poke_term_if.flush_fn;
+  TT (3);
+
+  tif.puts_fn = poke_term_if.puts_fn;
+  TT (5);
+
+  tif.printf_fn = poke_term_if.printf_fn;
+  TT (6);
+
+  tif.indent_fn = poke_term_if.indent_fn;
+  TT (7);
+
+  tif.class_fn = poke_term_if.class_fn;
+  TT (8);
+
+  tif.end_class_fn = poke_term_if.end_class_fn;
+  TT (9);
+
+  tif.hyperlink_fn = poke_term_if.hyperlink_fn;
+  TT (10);
+
+  tif.end_hyperlink_fn = poke_term_if.end_hyperlink_fn;
+  pkc = pk_compiler_new (&tif);
+  T ("pk_compiler_new_11", pkc != NULL);
+
+#undef TT
+
+  return pkc;
+}
+
+static void
+test_pk_compiler_free (pk_compiler pkc)
+{
+  pk_compiler_free (NULL);
+  pk_compiler_free (pkc);
+}
+
+int
+main ()
+{
+  pk_compiler pkc;
+
+  pkc = test_pk_compiler_new ();
+
+  test_pk_compiler_free (pkc);
+
+  return 0;
+}
diff --git a/testsuite/poke.libpoke/libpoke.exp 
b/testsuite/poke.libpoke/libpoke.exp
index 9c6090ef..52702d9b 100644
--- a/testsuite/poke.libpoke/libpoke.exp
+++ b/testsuite/poke.libpoke/libpoke.exp
@@ -4,3 +4,6 @@ load_lib "poke.exp"
 if { [verified_host_execute "poke.libpoke/values"] ne "" } {
     fail "values had an execution error"
 }
+if { [verified_host_execute "poke.libpoke/api"] ne "" } {
+    fail "api had an execution error"
+}
diff --git a/testsuite/poke.libpoke/values.c b/testsuite/poke.libpoke/values.c
index 6c4cd9df..ec1e3c79 100644
--- a/testsuite/poke.libpoke/values.c
+++ b/testsuite/poke.libpoke/values.c
@@ -36,36 +36,113 @@
 void
 test_simple_values ()
 {
+  static const char *awesome = "Poke is awesome!";
+  const size_t bigstr_len = 1u << 20; /* 1 MiB */
   pk_val val;
+  pk_val mag;
+  pk_val unit;
+
+#define T(name, cond)                                                         \
+  do                                                                          \
+    {                                                                         \
+      if (cond)                                                               \
+        pass (name);                                                          \
+      else                                                                    \
+        fail (name);                                                          \
+    }                                                                         \
+  while (0)
+
+  /* Signed integers */
 
   /* Exceeding maximum number of bits supported in poke integers.  */
-  val = pk_make_int (0, 65);
-  if (val == PK_NULL)
-    pass ("make_int_1");
-  else
-    fail ("make_int_1");
+  T ("pk_make_int_0", pk_make_int (0, 65) == PK_NULL);
 
-  /* Integer getters, positive value.  */
   val = pk_make_int (666, 32);
-  if (pk_int_size (val) == 32)
-    pass ("pk_int_size_1");
-  else
-    fail ("pk_int_size_1");
-  if (pk_int_value (val) == 666)
-    pass ("pk_int_value_1");
-  else
-    fail ("pk_int_value_1");
+  T ("pk_make_int_1", val != PK_NULL);
+  T ("pk_int_value_1", pk_int_value (val) == 666);
+  T ("pk_int_size_1", pk_int_size (val) == 32);
 
-  /* Integer getters, negative value.  */
   val = pk_make_int (-666, 32);
-  if (pk_int_size (val) == 32)
-    pass ("pk_int_size_2");
-  else
-    fail ("pk_int_size_2");
-  if (pk_int_value (val) == -666)
-    pass ("pk_int_value_2");
-  else
-    fail ("pk_int_value_2");
+  T ("pk_make_int_2", val != PK_NULL);
+  T ("pk_int_value_2", pk_int_value (val) == -666);
+  T ("pk_int_size_2", pk_int_size (val) == 32);
+
+  /* Unsigned integers */
+
+  val = pk_make_uint (UINT64_MAX, 63);
+  T ("pk_make_uint_0", val != PK_NULL);
+  T ("pk_uint_value_0", pk_uint_value (val) == (UINT64_MAX >> 1));
+  T ("pk_uint_size_0", pk_uint_size (val) == 63);
+
+  val = pk_make_uint (0, 64);
+  T ("pk_make_uint_1", val != PK_NULL);
+  T ("pk_uint_value_1", pk_uint_value (val) == 0);
+  T ("pk_uint_size_1", pk_uint_size (val) == 64);
+
+  val = pk_make_uint (0xabcdef, 24);
+  T ("pk_make_uint_2", val != PK_NULL);
+  T ("pk_uint_value_2", pk_uint_value (val) == 0xabcdef);
+  T ("pk_uint_size_2", pk_uint_size (val) == 24);
+
+  /* Strings */
+
+  val = pk_make_string (awesome);
+  T ("pk_make_string_0", val != PK_NULL);
+  T ("pk_string_str_0", strcmp (pk_string_str (val), awesome) == 0);
+
+  {
+    char *bigstr;
+
+    if ((bigstr = (char *)malloc (bigstr_len + 1)) == NULL)
+      err (1, "malloc () failed");
+    memset (bigstr, 'P', bigstr_len);
+    bigstr[bigstr_len] = '\0';
+
+    val = pk_make_string (bigstr);
+
+    memset (bigstr, 'p', bigstr_len);
+    free (bigstr);
+  }
+  T ("pk_make_string_1", val != PK_NULL);
+  {
+    const char *sb = pk_string_str (val);
+    const char *se = sb;
+
+    while (*se != '\0' && *se == 'P')
+      ++se;
+
+    T ("pk_string_str_1", se - sb == bigstr_len);
+  }
+
+  /* Offsets */
+
+  mag = pk_make_uint (0, 64);
+  unit = pk_make_int (1, 64);
+
+  assert (mag != PK_NULL);
+  assert (unit != PK_NULL);
+
+  val = pk_make_offset (mag, unit);
+  T ("pk_make_offset_0", val == PK_NULL); /* Because of signed unit */
+
+  unit = pk_make_uint (0, 64);
+  assert (unit != PK_NULL);
+
+  val = pk_make_offset (mag, unit);
+  T ("pk_make_offset_1", val == PK_NULL);
+
+  mag = pk_make_uint (UINT64_MAX, 64);
+  unit = pk_make_uint (UINT64_MAX, 64);
+  assert (mag != PK_NULL);
+  assert (unit != PK_NULL);
+
+  val = pk_make_offset (mag, unit);
+  T ("pk_make_offset_2", val != PK_NULL);
+  T ("pk_offset_magnitude_2",
+     pk_uint_value (pk_offset_magnitude (val)) == UINT64_MAX);
+  T ("pk_offset_unit_2", pk_uint_value (pk_offset_unit (val)) == UINT64_MAX);
+
+#undef T
 }
 
 #define STARTS_WITH(PREFIX, STR) (strncmp (PREFIX, STR, strlen (PREFIX)) == 0)
-- 
2.29.2



reply via email to

[Prev in Thread] Current Thread [Next in Thread]