/* * $Header: /home/gene/library/website/docsrc/test-rpc/src/src/RCS/test0010.c,v 395.1 2008/04/20 17:25:55 gene Exp $ * * Copyright (c) 2006 Gene Michael Stover. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 * USA */ /* * Test that we can XDR encode bool_t values to a temporary * file, then read them again. * For this test to be valid, we must already now that XDR * decodes bool_t values correctly. */ #include "this.h" /* */ static char S_pathname[] = "./tmp/test0010.data"; /* */ static bool_t S_a[] = { 0, 1, 2, 0}; static int S_alen = sizeof S_a / sizeof S_a[0]; /* */ static int S_Write () { int rc = 0; FILE *fp; XDR xdr; int i; fp = fopen (S_pathname, "wb"); if (fp != NULL) { xdrstdio_create (&xdr, fp, XDR_ENCODE); for (i = 0; rc == 0 && i < S_alen; ++i) { if (xdr_bool (&xdr, &S_a[i])) { /* good so far */ } else { printf ("\n%s:%d: xdr_bool: %s", __FILE__, __LINE__, strerror (errno)); printf ("%s:%d: xdr_bool failed for a[%d] = %d.", __FILE__, __LINE__, i, S_a[i]); rc = -26; } } xdr_destroy (&xdr); fclose (fp); } else { printf ("\n%s:%d: fopen: %s", __FILE__, __LINE__, strerror (errno)); printf ("%s:%d: Could not open \"%s\" for binary output.", __FILE__, __LINE__, S_pathname); rc = 22; } return rc; } /* * The two Boolean values are the same Boolean if both are * true or both are false. Otherwise, they are different * Boolean values. In other words, logical NEXOR. (Can't * use a bit-wise NEXOR.) */ static bool_t S_SameBoolean (bool_t a, bool_t b) { return (a && b) || (!a && !b); } /* */ static int S_Read () { int rc = 0; FILE *fp; XDR xdr; int i; bool_t b; fp = fopen (S_pathname, "rb"); if (fp != NULL) { xdrstdio_create (&xdr, fp, XDR_DECODE); for (i = 0; rc == 0 && i < S_alen; ++i) { if (xdr_bool (&xdr, &b)) { if (S_SameBoolean (b, S_a[i])) { /* good so far */ } else { printf ("\n%s:%d: xdr_bool decoded %d.", __FILE__, __LINE__, b); printf (" Expected a[%d] = %d.", i, S_a[i]); rc = -47; } } else { printf ("\n%s:%d: xdr_bool: %s", __FILE__, __LINE__, strerror (errno)); rc = -46; } } xdr_destroy (&xdr); fclose (fp); } else { printf ("\n%s:%d: fopen: %s", __FILE__, __LINE__, strerror (errno)); printf ("%s:%d: Could not open \"%s\" for binary input.", __FILE__, __LINE__, S_pathname); rc = 45; } return rc; } int main () { int rc = 0; if (S_Write () == 0) { if (S_Read () == 0) { /* good */ } else { printf ("\n%s:%d: S_Read failed", __FILE__, __LINE__); rc = -31; } } else { printf ("\n%s:%d: S_Write failed", __FILE__, __LINE__); rc = 31; } remove (S_pathname); return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } /* --- end of file --- */