OSSP CVS Repository

ossp - Check-in [733]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 733
Date: 2001-Aug-22 22:22:41 (local)
2001-Aug-22 20:22:41 (UTC)
User:simons
Branch:
Comment: Added test suite for float encoding/decoding.
Tickets:
Inspections:
Files:
ossp-pkg/xds/xds_test_xdr.c      1.5 -> 1.6     100 inserted, 0 deleted

ossp-pkg/xds/xds_test_xdr.c 1.5 -> 1.6

--- xds_test_xdr.c       2001/08/13 19:48:02     1.5
+++ xds_test_xdr.c       2001/08/22 20:22:41     1.6
@@ -734,3 +734,103 @@
 
 #endif /* XDS_TEST_XDR_OCTETSTREAM_EMPTY */
 
+#ifdef XDS_TEST_XDR_FLOAT
+
+int main(int argc, char *argv[])
+{
+    XDR xdrs;
+    char xdr_buf[1024];
+    size_t xdr_buf_size;
+
+    xds_t *xds;
+    char *xds_buf;
+    size_t xds_buf_size;
+
+    float values[] =
+        {
+        0,
+        3.14,
+        -3.14,
+        0.14,
+        -0.14,
+        123456.789,
+        -123456.789
+        };
+
+    size_t i;
+
+    /* Encode the values array using the RPC-XDR implementation. */
+    xdrmem_create(&xdrs, xdr_buf, sizeof (xdr_buf), XDR_ENCODE);
+    for (i = 0; i < sizeof (values) / sizeof (float); ++i)
+        xdr_float(&xdrs, &values[i]);
+    xdr_buf_size = xdr_getpos(&xdrs);
+    xdr_destroy(&xdrs);
+
+    /* Encode the values array using the XDS implementation. */
+    xds = xds_init(XDS_ENCODE);
+    if (xds == NULL) {
+        printf("Failed to initialize XDS context.\n");
+        return 1;
+    }
+    if (xds_register(xds, "float", &xdr_encode_float, NULL) != XDS_OK) {
+        printf("Failed to register my encoding engines.\n");
+        return 1;
+    }
+    for (i = 0; i < sizeof (values) / sizeof (float); ++i) {
+        if (xds_encode(xds, "float", values[i]) != XDS_OK) {
+            printf("xds_encode(values[%d]) failed!\n", i);
+            return 1;
+        }
+    }
+    if (xds_getbuffer(xds, XDS_GIFT, (void **)&xds_buf, &xds_buf_size) !=
+        XDS_OK) {
+        printf("getbuffer() failed.\n");
+        return 1;
+    }
+    xds_destroy(xds);
+
+    /* Both buffers must be equal now. */
+    if (xdr_buf_size != xds_buf_size) {
+        printf("The buffer sizes don't match: %d != %d.\n", xdr_buf_size,
+               xds_buf_size);
+        return 1;
+    }
+    if (memcmp(xds_buf, xdr_buf, xds_buf_size) != 0) {
+        printf("The buffers' contents is not identical!\n");
+        return 1;
+    }
+
+    /* Now we decode the values again using the XDS implementation and
+       compare them to our original values. Obviously, they should not
+       differ. */
+    xds = xds_init(XDS_DECODE);
+    if (xds == NULL) {
+        printf("Failed to initialize XDS context.\n");
+        return 1;
+    }
+    if (xds_register(xds, "float", &xdr_decode_float, NULL) != XDS_OK) {
+        printf("Failed to register my decoding engines.\n");
+        return 1;
+    }
+    if (xds_setbuffer(xds, XDS_GIFT, xds_buf, xds_buf_size) != XDS_OK) {
+        printf("setbuffer() failed.\n");
+        return 1;
+    }
+    for (i = 0; i < sizeof (values) / sizeof (float); ++i) {
+        float tmp;
+        if (xds_decode(xds, "float", &tmp) != XDS_OK) {
+            printf("xds_decode() failed for the %d value!\n", i);
+            return 1;
+        }
+        if (values[i] != tmp) {
+            printf("The %dth value has not been decoded correctly!\n", i);
+            return 1;
+        }
+    }
+    xds_destroy(xds);
+
+    /* Everything went fine. */
+    return 0;
+}
+
+#endif /* XDS_TEST_XDR_FLOAT */

CVSTrac 2.0.1