home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-03-04 | 8.2 KB | 329 lines | [TEXT/ALFA] |
- // This may look like C code, but it is really -*- C++ -*-
- /*
- ************************************************************************
- *
- * Verify integer and bit I/O
- *
- * $Id: vendian_io.cc,v 2.3 1995/06/19 21:47:57 oleg Exp oleg $
- *
- ************************************************************************
- */
-
- #include "endian_io.h"
- #include <iostream.h>
- #include "std.h"
-
- #define NO_PIPES 1
-
- /*
- *------------------------------------------------------------------------
- * Reading and checking functions
- */
-
- static void read_and_check_long(EndianIn& i_stream, const long ethalon)
- {
- long read = i_stream.read_long("Reading a long integer");
- if( read != ethalon )
- _error("The read long int %d differs from what was expected %d",
- read,ethalon);
- }
-
- static void read_and_check_short(EndianIn& i_stream, const short ethalon)
- {
- short read = i_stream.read_short("Reading a short integer");
- if( read != ethalon )
- _error("The read short int %d differs from what was expected %d",
- read,ethalon);
- }
-
- static void read_and_check_byte(EndianIn& i_stream, const char ethalon)
- {
- char read = i_stream.read_byte("Reading a byte");
- if( read != ethalon )
- _error("The read byte %d differs from what was expected %d",
- read,ethalon);
- }
-
- /*
- *------------------------------------------------------------------------
- * Reading and writing ethalon patterns
- */
-
- static const unsigned long MyPattern [] =
- { 1, (unsigned)-1, 0, 0xffff0000, 0x0000ffff, 0x5a5a5a5a, 0xa5a5a5a5 };
-
- static void write_patterns(EndianOut& o_stream)
- {
- {
- unsigned long * p = (unsigned long *)MyPattern;
- for(; (char *)p < (char *)MyPattern + sizeof(MyPattern); p++)
- o_stream.write_long(*p);
- }
- {
- unsigned short * p = (unsigned short *)MyPattern;
- for(; (char *)p < (char *)MyPattern + sizeof(MyPattern); p++)
- o_stream.write_short(*p);
- }
- {
- unsigned char * p = (unsigned char *)MyPattern;
- for(; (char *)p < (char *)MyPattern + sizeof(MyPattern); p++)
- o_stream.write_byte(*p);
- }
- }
-
- static void read_and_check_patterns(EndianIn& i_stream)
- {
- {
- unsigned long * p = (unsigned long *)MyPattern;
- for(; (char *)p < (char *)MyPattern + sizeof(MyPattern); p++)
- read_and_check_long(i_stream,*p);
- }
- {
- unsigned short * p = (unsigned short *)MyPattern;
- for(; (char *)p < (char *)MyPattern + sizeof(MyPattern); p++)
- read_and_check_short(i_stream,*p);
- }
- {
- unsigned char * p = (unsigned char *)MyPattern;
- for(; (char *)p < (char *)MyPattern + sizeof(MyPattern); p++)
- read_and_check_byte(i_stream,*p);
- }
- }
-
- /*
- *------------------------------------------------------------------------
- * Verify reading/writing integers in
- * littleendian mode (most significant byte last)
- */
-
- static void test_littleendian()
- {
- cout << "\n--> Test reading/writing integers in the littleendian mode\n";
-
- cout << "Opening the output stream to file /tmp/aa\n";
- EndianOut stream("/tmp/aa");
- stream.set_littlendian();
-
- cout << "Writing patterns\n";
- write_patterns(stream);
- stream.close();
-
- cout << "Opening the file as a reading stream through cat\n";
- EndianIn istream;
- #if defined(NO_PIPES) && NO_PIPES
- istream.open("/tmp/aa");
- #else
- istream.open("cat /tmp/aa |");
- #endif
- istream.set_littlendian();
-
- cout << "Reading what we've written back\n";
- read_and_check_patterns(istream);
- istream.close();
-
- cout << "\nDone\n";
- }
-
- /*
- *------------------------------------------------------------------------
- * Verify reading/writing integers in
- * bigendian mode (most significant byte first)
- */
-
- static void test_bigendian()
- {
- cout << "\n--> Test reading/writing integers in the bigendian mode\n";
-
- {
- cout << "Opening the output stream to file /tmp/aa through cat\n";
- #if defined(NO_PIPES) && NO_PIPES
- EndianOut stream("/tmp/aa");
- #else
- EndianOut stream("| cat > /tmp/aa");
- #endif
- stream.set_bigendian();
-
- cout << "Writing patterns\n";
- write_patterns(stream);
- // Stream should be closed upon destruction
- }
-
- cout << "Opening the file as a reading stream straight\n";
- EndianIn istream;
- sleep(1);
- istream.open("/tmp/aa");
- istream.set_bigendian();
-
- cout << "Reading what we've written back\n";
- read_and_check_patterns(istream);
- istream.close();
-
- cout << "\nDone\n";
- }
-
- /*
- *------------------------------------------------------------------------
- * Test the mixed int/bit stream I/O
- */
-
- static void test_int_bit_IO()
- {
- cout << "\n--> Test mixed int/bit stream I/O with file attachments\n";
-
- cout << "Opening the output stream to file /tmp/aa through cat\n";
- #if defined(NO_PIPES) && NO_PIPES
- EndianOut stream("/tmp/aa");
- #else
- EndianOut stream("| cat > /tmp/aa");
- #endif
- stream.set_bigendian();
-
- cout << "Writing integer patterns\n";
- write_patterns(stream);
-
- cout << "Attaching the bitstream\n";
- BitOut bitstream;
- bitstream.share_with(stream);
-
- cout << "Writing 8 bits of ones followed by 3*8 zero bits several times\n";
- register int i;
- for(i=0; i<5; i++)
- {
- register int i;
- for(i=0; i<8; i++)
- bitstream.put_bit(1);
- for(i=0; i<3*8; i++)
- bitstream.put_bit(0);
- }
- bitstream.put_bit(1); // Put two extra bits
- bitstream.put_bit(1);
- bitstream.close();
- {
- cout << "Attaching the second bitstream\n";
- BitOut bitstream;
- bitstream.share_with(stream);
-
- cout << "Writing 8 bits of zeros followed by 3*8 one bits several times\n";
- register int i;
- for(i=0; i<5; i++)
- {
- register int i;
- for(i=0; i<8; i++)
- bitstream.put_bit(0);
- for(i=0; i<3*8; i++)
- bitstream.put_bit(1);
- }
- bitstream.put_bit(0); // Put two extra bits
- bitstream.put_bit(1);
- }
- stream.close();
-
- cout << "Opening the file as a reading stream straight\n";
- EndianIn istream;
- sleep(1);
- istream.open("/tmp/aa");
- istream.set_bigendian();
-
- cout << "Reading what we've written back\n";
- read_and_check_patterns(istream);
-
- cout << "Attaching the input bitstream\n";
- BitIn ibitstream;
- ibitstream.share_with(istream);
-
- #ifndef __MWERKS__
- system("ls -l /tmp/aa; od -x /tmp/aa");
- #endif
- cout << "Reading the bit pattern\n";
- for(i=0; i<5; i++)
- {
- register int i;
- for(i=0; i<8; i++)
- assert( ibitstream.get_bit() == 1 );
- for(i=0; i<3*8; i++)
- assert( ibitstream.get_bit() == 0 );
- }
- assert( ibitstream.get_bit() == 1 );
- assert( ibitstream.get_bit() == 1 );
- ibitstream.close();
- {
- cout << "Attaching the second input bitstream\n";
- BitIn ibitstream;
- ibitstream.share_with(istream);
-
- cout << "Reading the bit pattern\n";
- for(i=0; i<5; i++)
- {
- register int i;
- for(i=0; i<8; i++)
- assert( ibitstream.get_bit() == 0 );
- for(i=0; i<3*8; i++)
- assert( ibitstream.get_bit() == 1 );
- }
- assert( ibitstream.get_bit() == 0 );
- assert( ibitstream.get_bit() == 1 );
- }
- cout << "\nDone\n";
- }
-
- /*
- *------------------------------------------------------------------------
- * Test the mixed int/bit stream I/O
- */
-
- static void test_varbit_int(void)
- {
- cout << "\n--> Test reading/writing short ints using variable number of bits"
- << endl;
-
- const short ethalon [] =
- { 0, 1, 2, -1, -31, -17, 31, 15, -32, 63, 10000, 64+512, 64+511,
- -64-511, -4096, -3, -64-512-4095, 64+512+4096, 0, 1
- };
-
- {
- BitOut outbs;
- outbs.open("/tmp/aa");
- assert( outbs.good() );
- for(register unsigned int i=0; i<sizeof(ethalon)/sizeof(ethalon[0]); i++)
- outbs.put_short(ethalon[i]);
- outbs.put_bit(1); // just for the heck of it
- outbs.put_bit(1);
- outbs.put_bit(1);
- }
-
- {
- BitIn inbs;
- inbs.open("/tmp/aa");
- assert( inbs.good() );
- for(register unsigned int i=0; i<sizeof(ethalon)/sizeof(ethalon[0]); i++)
- {
- short val_read = inbs.get_short();
- if( val_read != ethalon[i] )
- _error("%d-th read value %d does not match the ethalon %d",
- i,val_read,ethalon[i]);
- }
- assert( inbs.get_bit() == 1 );
- assert( inbs.get_bit() == 1 );
- assert( inbs.get_bit() == 1 );
- inbs.close();
- }
-
- cout << "\nDone\n";
- }
-
- /*
- *------------------------------------------------------------------------
- * Root module
- */
-
- main()
- {
- cout << "\n\n\t\tVerify integer stream I/O in big/little endian modes"
- "\n\t\t\t\tand bit stream I/O\n\n";
- test_littleendian();
- test_bigendian();
- test_int_bit_IO();
- test_varbit_int();
- }
-