Author: massie
Date: Tue Nov  3 18:59:32 2009
New Revision: 832510

URL: http://svn.apache.org/viewvc?rev=832510&view=rev
Log:
AVRO-179. Add unit tests for all Avro C primitives

Added:
    hadoop/avro/trunk/src/c/m4/
    hadoop/avro/trunk/src/c/m4/.gitignore
    hadoop/avro/trunk/src/c/test_avro_primitives.c
Removed:
    hadoop/avro/trunk/src/c/INSTALL
Modified:
    hadoop/avro/trunk/CHANGES.txt
    hadoop/avro/trunk/src/c/.gitignore
    hadoop/avro/trunk/src/c/Makefile.am
    hadoop/avro/trunk/src/c/avro_double.c
    hadoop/avro/trunk/src/c/avro_endian.c
    hadoop/avro/trunk/src/c/avro_float.c
    hadoop/avro/trunk/src/c/avro_io_memory.c
    hadoop/avro/trunk/src/c/avro_primitives.c
    hadoop/avro/trunk/src/c/avro_private.h
    hadoop/avro/trunk/src/c/configure.in

Modified: hadoop/avro/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/CHANGES.txt?rev=832510&r1=832509&r2=832510&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Tue Nov  3 18:59:32 2009
@@ -38,7 +38,10 @@
     AVRO-149. Add Java command-line executable, "avroj".
     (Philip Zeyliger via cutting)
 
-    AVRO-175. Split the avro_io interface into two interfaces: avro_reader and 
avro_writer
+    AVRO-175. Split the avro_io interface into two interfaces: avro_reader 
+    and avro_writer (massie)
+
+    AVRO-179. Add units tests for all Avro C primitives (massie)
 
   OPTIMIZATIONS
 

Modified: hadoop/avro/trunk/src/c/.gitignore
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c/.gitignore?rev=832510&r1=832509&r2=832510&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c/.gitignore (original)
+++ hadoop/avro/trunk/src/c/.gitignore Tue Nov  3 18:59:32 2009
@@ -6,3 +6,4 @@
 configure
 *.lo
 *.o
+INSTALL

Modified: hadoop/avro/trunk/src/c/Makefile.am
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c/Makefile.am?rev=832510&r1=832509&r2=832510&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c/Makefile.am (original)
+++ hadoop/avro/trunk/src/c/Makefile.am Tue Nov  3 18:59:32 2009
@@ -1,8 +1,10 @@
 # -pedantic
 AM_CFLAGS=$(APR_CFLAGS) $(APR_INCLUDES) $(APU_INCLUDES) -Wall 
+ACLOCAL_AMFLAGS=-I m4
+
 C_DOCS_OUTPUT ?= "docs/dox"
 
-EXTRA_DIST=json_parser.y lemon.c lempar.c json_tests avro_schema_tests docs 
version.sh
+EXTRA_DIST=json_parser.y lemon.c lempar.c json_tests avro_schema_tests docs 
version.sh m4
 
 include_HEADERS = avro.h
 
@@ -21,7 +23,7 @@
         -export-dynamic
 
 
-check_PROGRAMS=test_json_parser test_avro_schema
+check_PROGRAMS=test_json_parser test_avro_schema test_avro_primitives
 
 dist-hook: docs
 
@@ -33,6 +35,11 @@
 test_avro_schema=test_avro_schema.c
 test_avro_schema_LDADD=$(test_LDADD)
 
+test_avro_primitives=test_avro_primitives.c
+test_avro_primitives_LDADD=$(test_LDADD)
+# use -std=c99 to avoid warings about universal character names
+test_avro_primitives_CFLAGS=$(AM_CFLAGS) -std=c99
+
 #test_avro_interop=test_avro_interop.c
 #test_avro_interop_LDADD=$(test_LDADD)
 

Modified: hadoop/avro/trunk/src/c/avro_double.c
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c/avro_double.c?rev=832510&r1=832509&r2=832510&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c/avro_double.c (original)
+++ hadoop/avro/trunk/src/c/avro_double.c Tue Nov  3 18:59:32 2009
@@ -51,7 +51,7 @@
     }
   self->value_set = 1;
   dp = &self->value;
-  return avro_read_int64_le (reader->io, (int64_t *) dp);
+  return avro_read_double (reader->io, dp);
 }
 
 static avro_status_t
@@ -66,7 +66,7 @@
       return AVRO_FAILURE;
     }
   self->value_set = 0;
-  return avro_read_int64_le (reader->io, (int64_t *) dp);
+  return avro_read_double (reader->io, dp);
 }
 
 static avro_status_t
@@ -78,7 +78,7 @@
     {
       return AVRO_FAILURE;
     }
-  return avro_write_int64_le (writer->io, (int64_t) self->value);;
+  return avro_write_double (writer->io, self->value);
 }
 
 static struct avro_value *

Modified: hadoop/avro/trunk/src/c/avro_endian.c
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c/avro_endian.c?rev=832510&r1=832509&r2=832510&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c/avro_endian.c (original)
+++ hadoop/avro/trunk/src/c/avro_endian.c Tue Nov  3 18:59:32 2009
@@ -27,7 +27,7 @@
     {
       return AVRO_FAILURE;
     }
-  status = io->read (io, (char *) buf, sizeof (buf));
+  status = io->read (io, (void *) buf, sizeof (buf));
   if (status != AVRO_OK)
     {
       return status;
@@ -62,7 +62,7 @@
     {
       return AVRO_FAILURE;
     }
-  status = io->read (io, (char *) buf, sizeof (buf));
+  status = io->read (io, (void *) buf, sizeof (buf));
   if (status != AVRO_OK)
     {
       return status;
@@ -98,7 +98,7 @@
     {
       return AVRO_FAILURE;
     }
-  status = io->read (io, (char *) buf, sizeof (buf));
+  status = io->read (io, (void *) buf, sizeof (buf));
   if (status != AVRO_OK)
     {
       return status;
@@ -141,7 +141,7 @@
     {
       return AVRO_FAILURE;
     }
-  status = io->read (io, (char *) buf, sizeof (buf));
+  status = io->read (io, (void *) buf, sizeof (buf));
   if (status != AVRO_OK)
     {
       return status;

Modified: hadoop/avro/trunk/src/c/avro_float.c
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c/avro_float.c?rev=832510&r1=832509&r2=832510&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c/avro_float.c (original)
+++ hadoop/avro/trunk/src/c/avro_float.c Tue Nov  3 18:59:32 2009
@@ -51,7 +51,7 @@
     }
   fp = &self->value;
   self->value_set = 1;
-  return avro_read_int32_le (reader->io, (int32_t *) fp);
+  return avro_read_float (reader->io, fp);
 }
 
 static avro_status_t
@@ -66,7 +66,7 @@
       return AVRO_FAILURE;
     }
   self->value_set = 0;
-  return avro_read_int32_le (reader->io, (int32_t *) fp);
+  return avro_read_float (reader->io, fp);
 }
 
 static avro_status_t
@@ -78,7 +78,7 @@
     {
       return AVRO_FAILURE;
     }
-  return avro_write_int32_le (writer->io, (int32_t) self->value);
+  return avro_write_float (writer->io, self->value);
 }
 
 static struct avro_value *

Modified: hadoop/avro/trunk/src/c/avro_io_memory.c
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c/avro_io_memory.c?rev=832510&r1=832509&r2=832510&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c/avro_io_memory.c (original)
+++ hadoop/avro/trunk/src/c/avro_io_memory.c Tue Nov  3 18:59:32 2009
@@ -16,22 +16,21 @@
 specific language governing permissions and limitations
 under the License.
 */
-
 #include "avro_private.h"
 
 struct avro_io_memory_reader
 {
   void *addr;
-  uint64_t used;
-  uint64_t len;
+  avro_long_t used;
+  avro_long_t len;
   struct avro_io_reader io;
 };
 
 struct avro_io_memory_writer
 {
   void *addr;
-  uint64_t used;
-  uint64_t len;
+  avro_long_t used;
+  avro_long_t len;
   struct avro_io_writer io;
 };
 
@@ -46,6 +45,9 @@
     }
   memcpy (addr, self->addr + self->used, len);
   self->used += len;
+  DEBUG (fprintf
+        (stderr, "avro_io_memory_read %ld bytes, %ld/%ld used\n", len,
+         self->used, self->len));
   return AVRO_OK;
 }
 
@@ -59,6 +61,9 @@
       return AVRO_FAILURE;
     }
   self->used += len;
+  DEBUG (fprintf
+        (stderr, "avro_io_memory_skip %ld bytes, %ld/%ld used\n", len,
+         self->used, self->len));
   return AVRO_OK;
 }
 
@@ -67,12 +72,15 @@
 {
   struct avro_io_memory_writer *self =
     container_of (io, struct avro_io_memory_writer, io);
-  if ((self->len - self->used) < len)
+  if (len < 0 || (self->len - self->used) < len)
     {
       return AVRO_FAILURE;
     }
   memcpy (self->addr + self->used, addr, len);
   self->used += len;
+  DEBUG (fprintf
+        (stderr, "avro_io_memory_write %d bytes, %d/%d used\n", len,
+         self->used, self->len); dump (stderr, addr, len));
   return AVRO_OK;
 }
 

Modified: hadoop/avro/trunk/src/c/avro_primitives.c
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c/avro_primitives.c?rev=832510&r1=832509&r2=832510&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c/avro_primitives.c (original)
+++ hadoop/avro/trunk/src/c/avro_primitives.c Tue Nov  3 18:59:32 2009
@@ -177,7 +177,7 @@
 avro_write_int (struct avro_io_writer * io, avro_int_t * ip)
 {
   avro_status_t status;
-  int32_t n = *ip;
+  uint32_t n;
   uint8_t b;
 
   if (!io || !ip)
@@ -185,27 +185,27 @@
       return AVRO_FAILURE;
     }
 
-  /* move sign to low-order bit */
-  n = (n << 1) ^ (n >> 31);
+  n = (*ip << 1) ^ (*ip >> 31);
   while ((n & ~0x7F) != 0)
     {
       b = ((((uint8_t) n) & 0x7F) | 0x80);
-      status = io->write (io, (char *) &b, 1);
+      status = io->write (io, (void *) &b, 1);
       if (status != AVRO_OK)
        {
          return status;
        }
       n >>= 7;
     }
+
   b = (uint8_t) n;
-  return io->write (io, (char *) &b, 1);
+  return io->write (io, (void *) &b, 1);
 }
 
 avro_status_t
 avro_write_long (struct avro_io_writer *io, avro_long_t * lp)
 {
   avro_status_t status;
-  int64_t n = *lp;
+  uint64_t n;
   uint8_t b;
 
   if (!io || !lp)
@@ -213,12 +213,11 @@
       return AVRO_FAILURE;
     }
 
-  /* move sign to low-order bit */
-  n = (n << 1) ^ (n >> 63);
+  n = (*lp << 1) ^ (*lp >> 63);
   while ((n & ~0x7F) != 0)
     {
       b = ((((uint8_t) n) & 0x7F) | 0x80);
-      status = io->write (io, (char *) &b, 1);
+      status = io->write (io, (void *) &b, 1);
       if (status != AVRO_OK)
        {
          return status;
@@ -226,14 +225,14 @@
       n >>= 7;
     }
   b = (uint8_t) n;
-  return io->write (io, (char *) &b, 1);
+  return io->write (io, (void *) &b, 1);
 }
 
 avro_status_t
 avro_read_int (struct avro_io_reader *io, avro_int_t * ip)
 {
   avro_status_t status;
-  int64_t value = 0;
+  uint32_t value = 0;
   int offset = 0;
   uint8_t b;
   const int MAX_VARINT_BUF_SIZE = 5;
@@ -258,7 +257,6 @@
       ++offset;
     }
   while (b & 0x80);
-  /* back to two's-complement value; */
   *ip = (value >> 1) ^ -(value & 1);
   return AVRO_OK;
 }
@@ -267,7 +265,7 @@
 avro_read_long (struct avro_io_reader * io, avro_long_t * lp)
 {
   avro_status_t status;
-  int64_t value = 0;
+  uint64_t value = 0;
   int offset = 0;
   uint8_t b;
   const int MAX_VARINT_BUF_SIZE = 10;
@@ -292,7 +290,82 @@
       ++offset;
     }
   while (b & 0x80);
-  /* back to two's-complement value; */
   *lp = (value >> 1) ^ -(value & 1);
   return AVRO_OK;
 }
+
+avro_status_t
+avro_write_float (struct avro_io_writer * io, float value)
+{
+  union
+  {
+    float f;
+    int32_t i;
+  } v;
+  if (!io)
+    {
+      return AVRO_FAILURE;
+    }
+  v.f = value;
+  return avro_write_int32_le (io, (const int32_t) v.i);
+}
+
+avro_status_t
+avro_read_float (struct avro_io_reader *io, float *fp)
+{
+  avro_status_t status;
+  union
+  {
+    float f;
+    int32_t i;
+  } v;
+  if (!io || !fp)
+    {
+      return AVRO_FAILURE;
+    }
+  status = avro_read_int32_le (io, &v.i);
+  if (status != AVRO_OK)
+    {
+      return status;
+    }
+  *fp = v.f;
+  return AVRO_OK;
+}
+
+avro_status_t
+avro_write_double (struct avro_io_writer * io, double value)
+{
+  union
+  {
+    double d;
+    int64_t i;
+  } v;
+  if (!io)
+    {
+      return AVRO_FAILURE;
+    }
+  v.d = value;
+  return avro_write_int64_le (io, v.i);
+}
+
+avro_status_t
+avro_read_double (struct avro_io_reader * io, double *dp)
+{
+  avro_status_t status;
+  union
+  {
+    double d;
+    int64_t i;
+  } v;
+  if (!io || !dp)
+    {
+      return AVRO_FAILURE;
+    }
+  status = avro_read_int64_le (io, &v.i);
+  if (status != AVRO_OK)
+    {
+      return AVRO_FAILURE;
+    }
+  *dp = v.d;
+  return AVRO_OK;
+}

Modified: hadoop/avro/trunk/src/c/avro_private.h
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c/avro_private.h?rev=832510&r1=832509&r2=832510&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c/avro_private.h (original)
+++ hadoop/avro/trunk/src/c/avro_private.h Tue Nov  3 18:59:32 2009
@@ -202,8 +202,15 @@
 avro_status_t avro_read_int (struct avro_io_reader *reader, avro_int_t * ip);
 avro_status_t avro_read_long (struct avro_io_reader *reader,
                              avro_long_t * lp);
+avro_status_t avro_write_float (struct avro_io_writer *io, float value);
+avro_status_t avro_read_float (struct avro_io_reader *io, float *fp);
+avro_status_t avro_write_double (struct avro_io_writer *io, double value);
+avro_status_t avro_read_double (struct avro_io_reader *io, double *fp);
 
+
+#ifndef DEBUGGING
 #define DEBUGGING 0
+#endif
 
 #if DEBUGGING
 #define DEBUG(__cmd) __cmd

Modified: hadoop/avro/trunk/src/c/configure.in
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c/configure.in?rev=832510&r1=832509&r2=832510&view=diff
==============================================================================
--- hadoop/avro/trunk/src/c/configure.in (original)
+++ hadoop/avro/trunk/src/c/configure.in Tue Nov  3 18:59:32 2009
@@ -6,6 +6,7 @@
 AC_CONFIG_AUX_DIR([config])
 AM_INIT_AUTOMAKE
 AC_CONFIG_SRCDIR([avro.h])
+AC_CONFIG_MACRO_DIR([m4])
 AC_CONFIG_HEADER([config.h])
 
 LIBAVRO_VERSION=m4_esyscmd([./version.sh libtool])
@@ -14,6 +15,7 @@
 
 # Checks for programs.
 AC_PROG_CC
+AM_PROG_CC_C_O
 AC_PROG_LIBTOOL
 
 # Checks for libraries.

Added: hadoop/avro/trunk/src/c/m4/.gitignore
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c/m4/.gitignore?rev=832510&view=auto
==============================================================================
--- hadoop/avro/trunk/src/c/m4/.gitignore (added)
+++ hadoop/avro/trunk/src/c/m4/.gitignore Tue Nov  3 18:59:32 2009
@@ -0,0 +1 @@
+*

Added: hadoop/avro/trunk/src/c/test_avro_primitives.c
URL: 
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/c/test_avro_primitives.c?rev=832510&view=auto
==============================================================================
--- hadoop/avro/trunk/src/c/test_avro_primitives.c (added)
+++ hadoop/avro/trunk/src/c/test_avro_primitives.c Tue Nov  3 18:59:32 2009
@@ -0,0 +1,413 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+#include <stdlib.h>
+#include <time.h>
+#include <string.h>
+#include "avro_private.h"
+#include "dump.h"
+
+char buf[1024];
+
+typedef avro_status_t (*avro_test) (apr_pool_t * pool,
+                                   avro_io_reader * reader,
+                                   avro_io_writer * writer);
+struct test_case
+{
+  avro_long_t value;
+  unsigned len;
+  uint8_t bytes[16];
+};
+typedef struct test_case test_case;
+
+struct test_case test_cases[] = {
+  {.value = 0,.len = 1,.bytes = {0x0}},
+  {.value = -1,.len = 1,.bytes = {0x1}},
+  {.value = 1,.len = 1,.bytes = {0x2}},
+  {.value = -2,.len = 1,.bytes = {0x3}},
+  {.value = 2,.len = 1,.bytes = {0x4}},
+  {.value = -64,.len = 1,.bytes = {0x7f}},
+  {.value = 64,.len = 2,.bytes = {0x80, 0x01}},
+  {.value = -65,.len = 2,.bytes = {0x81, 0x01}},
+  {.value = 65,.len = 2,.bytes = {0x82, 0x01}}
+};
+
+static avro_status_t
+test_string (apr_pool_t * pool, avro_io_reader * reader,
+            avro_io_writer * writer)
+{
+  int i;
+  avro_status_t status;
+  avro_string_t strings[] = { L"\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
+    
L"\\\"\ucafe\ubabe\uab98\ufcde\ubcda\uef4a\b\f\n\r\t`...@#$%^&*()_+-=[]{}|;:',./<>?",
+    L"foo", L"bar", L"baz"
+  };
+  avro_string_t read_str;
+
+  for (i = 0; i < sizeof (strings) / sizeof (strings[0]); i++)
+    {
+      avro_string_t str = strings[i];
+      status = avro_write_string (writer, pool, str);
+      if (status != AVRO_OK)
+       {
+         return status;
+       }
+      status = avro_read_string (reader, pool, &read_str);
+      if (status != AVRO_OK)
+       {
+         return status;
+       }
+      if (wcscmp (str, read_str))
+       {
+         /* string didn't survive encoding/decoding */
+         fprintf (stderr, "%ls != %ls\n", str, read_str);
+         return AVRO_FAILURE;
+       }
+      else
+       {
+         fprintf (stderr, "%ls = %ls\n", str, read_str);
+       }
+    }
+  return AVRO_OK;
+}
+
+static avro_status_t
+test_bytes (apr_pool_t * pool, avro_io_reader * reader,
+           avro_io_writer * writer)
+{
+  avro_status_t status;
+  char bytes[] = { 0xDE, 0xAD, 0xBE, 0xEF };
+  char *read_bytes;
+  avro_long_t len;
+
+  status = avro_write_bytes (writer, bytes, sizeof (bytes));
+  if (status != AVRO_OK)
+    {
+      return status;
+    }
+  status = avro_read_bytes (reader, pool, (void *) &read_bytes, &len);
+  if (status != AVRO_OK)
+    {
+      return status;
+    }
+  if (len != sizeof (bytes))
+    {
+      return AVRO_FAILURE;
+    }
+  if (memcmp (bytes, read_bytes, len))
+    {
+      return AVRO_FAILURE;
+    }
+  return AVRO_OK;
+}
+
+static avro_status_t
+test_int_long (apr_pool_t * pool, avro_io_reader * reader,
+              avro_io_writer * writer, int is_long)
+{
+  apr_pool_t *subpool;
+  avro_status_t status;
+  int i;
+  const int num_rand_tests = 100;
+  uint8_t my_buf[64];
+  avro_io_writer *mywriter;
+  long rand_val;
+
+  apr_pool_create (&subpool, pool);
+  for (i = 0; i < sizeof (test_cases) / sizeof (test_cases[0]); i++)
+    {
+      struct test_case *test = test_cases + i;
+      avro_int_t int_test_val;
+      avro_long_t long_test_val;
+
+      mywriter =
+       avro_io_writer_from_memory (subpool, (void *) my_buf,
+                                   sizeof (my_buf));
+      if (!mywriter)
+       {
+         return AVRO_FAILURE;
+       }
+
+      if (is_long)
+       {
+         long_test_val = test->value;
+         DEBUG (fprintf
+                (stderr, "Running long test val=%lld\n", long_test_val));
+         status = avro_write_long (mywriter, &long_test_val);
+       }
+      else
+       {
+         int_test_val = (avro_int_t) test->value;
+         DEBUG (fprintf (stderr, "Running int test val=%d\n", int_test_val));
+         status = avro_write_int (mywriter, &int_test_val);
+       }
+      if (status != AVRO_OK)
+       {
+         return status;
+       }
+      if (memcmp ((void *) my_buf, (void *) test->bytes, test->len))
+       {
+         fprintf (stderr, "Received bytes");
+         dump (stderr, (void *) my_buf, test->len);
+         fprintf (stderr, "Expected bytes");
+         dump (stderr, (void *) test->bytes, test->len);
+         return AVRO_FAILURE;
+       }
+      apr_pool_clear (subpool);
+    }
+
+  for (i = 0; i < num_rand_tests; i++)
+    {
+      avro_long_t long_in, long_out;
+      avro_int_t int_in, int_out, *intp;
+      rand_val = random ();
+      if (is_long)
+       {
+         intp = (avro_int_t *) & long_in;
+         intp[0] = random ();
+         intp[1] = random ();
+       }
+      else
+       {
+         int_in = random ();
+       }
+      if (rand_val % 2)
+       {
+         long_in -= (long_in * 2);
+         int_in -= (int_in * 2);
+       }
+      if (is_long)
+       {
+         DEBUG (fprintf
+                (stderr, "Running long test %d val=%lld\n", i, long_in));
+         status = avro_write_long (writer, &long_in);
+         if (status != AVRO_OK)
+           {
+             fprintf (stderr, "Unable to write long %lld\n", long_in);
+             return status;
+           }
+         status = avro_read_long (reader, &long_out);
+         if (status != AVRO_OK)
+           {
+             fprintf (stderr, "Unable to read long %lld\n", long_in);
+             return status;
+           }
+         if (long_in != long_out)
+           {
+             fprintf (stderr, "%lld != %lld\n", long_in, long_out);
+             return AVRO_FAILURE;
+           }
+       }
+      else
+       {
+         DEBUG (fprintf (stderr, "Running int test %d val=%d\n", i, int_in));
+         status = avro_write_int (writer, &int_in);
+         if (status != AVRO_OK)
+           {
+             fprintf (stderr, "Unable to write int %d\n", int_in);
+             return status;
+           }
+         status = avro_read_int (reader, &int_out);
+         if (status != AVRO_OK)
+           {
+             fprintf (stderr, "Unable to read int\n");
+             return status;
+           }
+         if (int_in != int_out)
+           {
+             fprintf (stderr, "%d != %d\n", int_in, int_out);
+             return AVRO_FAILURE;
+           }
+       }
+    }
+  return AVRO_OK;
+}
+
+static avro_status_t
+test_int (apr_pool_t * pool, avro_io_reader * reader, avro_io_writer * writer)
+{
+  return test_int_long (pool, reader, writer, 0);
+}
+
+static avro_status_t
+test_long (apr_pool_t * pool, avro_io_reader * reader,
+          avro_io_writer * writer)
+{
+  return test_int_long (pool, reader, writer, 1);
+}
+
+static avro_status_t
+test_float (apr_pool_t * pool, avro_io_reader * reader,
+           avro_io_writer * writer)
+{
+  avro_status_t status;
+  float input, output;
+  int i;
+  int const num_rand_tests = 25;
+
+  for (i = 0; i < num_rand_tests; i++)
+    {
+      input = random () * 1.1;
+      status = avro_write_float (writer, input);
+      if (status != AVRO_OK)
+       {
+         return status;
+       }
+      status = avro_read_float (reader, &output);
+      if (status != AVRO_OK)
+       {
+         return status;
+       }
+      if (input != output)
+       {
+         fprintf (stderr, "%f != %f\n", input, output);
+         return AVRO_FAILURE;
+       }
+    }
+  return AVRO_OK;
+}
+
+static avro_status_t
+test_double (apr_pool_t * pool, avro_io_reader * reader,
+            avro_io_writer * writer)
+{
+  avro_status_t status;
+  double input, output;
+  int i;
+  int const num_rand_tests = 25;
+
+  for (i = 0; i < num_rand_tests; i++)
+    {
+      input = random () * 1.1;
+      status = avro_write_double (writer, input);
+      if (status != AVRO_OK)
+       {
+         return status;
+       }
+      status = avro_read_double (reader, &output);
+      if (status != AVRO_OK)
+       {
+         return status;
+       }
+      if (input != output)
+       {
+         fprintf (stderr, "%f != %f\n", input, output);
+         return AVRO_FAILURE;
+       }
+    }
+  return AVRO_OK;
+}
+
+static avro_status_t
+test_boolean (apr_pool_t * pool, avro_io_reader * reader,
+             avro_io_writer * writer)
+{
+  avro_status_t status;
+  int i, bool_in, bool_out;
+
+  for (i = 0; i < 2; i++)
+    {
+      bool_in = i;
+      status = avro_write_bool (writer, bool_in);
+      if (status != AVRO_OK)
+       {
+         return status;
+       }
+      status = avro_read_bool (reader, &bool_out);
+      if (status != AVRO_OK)
+       {
+         return status;
+       }
+      if (bool_in != bool_out)
+       {
+         fprintf (stderr, "%d != %d\n", bool_in, bool_out);
+         return AVRO_FAILURE;
+       }
+    }
+  return AVRO_OK;
+}
+
+static avro_status_t
+test_null (apr_pool_t * pool, avro_io_reader * reader,
+          avro_io_writer * writer)
+{
+  return AVRO_OK;
+}
+
+int
+main (void)
+{
+  int i;
+  apr_pool_t *pool, *subpool;
+  avro_status_t status;
+  avro_io_reader *reader;
+  avro_io_writer *writer;
+  struct avro_tests
+  {
+    char *name;
+    avro_test func;
+  } tests[] =
+  {
+    {
+    "string", test_string},
+    {
+    "bytes", test_bytes},
+    {
+    "int", test_int},
+    {
+    "long", test_long},
+    {
+    "float", test_float},
+    {
+    "double", test_double},
+    {
+    "boolean", test_boolean},
+    {
+    "null", test_null}
+  };
+
+  srandom (time (NULL));
+
+  avro_initialize ();
+  apr_pool_create (&pool, NULL);
+  for (i = 0; i < sizeof (tests) / sizeof (tests[0]); i++)
+    {
+      struct avro_tests *test = tests + i;
+      apr_pool_create (&subpool, pool);
+      reader = avro_io_reader_from_memory (subpool, buf, sizeof (buf));
+      if (!reader)
+       {
+         return AVRO_FAILURE;
+       }
+      writer = avro_io_writer_from_memory (subpool, buf, sizeof (buf));
+      if (!writer)
+       {
+         return AVRO_FAILURE;
+       }
+      fprintf (stderr, "Running %s tests...\n", test->name);
+      status = test->func (subpool, reader, writer);
+      if (status != AVRO_OK)
+       {
+         fprintf (stderr, "failed!\n");
+         return EXIT_FAILURE;
+       }
+      fprintf (stderr, "\t... %s tests passed!\n", test->name);
+    }
+  apr_pool_destroy (pool);
+  return EXIT_SUCCESS;
+}


Reply via email to