void test_verybasic()
{
	qio_file_t *f = NULL;
	qio_channel_t *writing = NULL;
	qio_channel_t *reading = NULL;
	err_t err; 
	char buf[4] = {0};
	
	//open the tmp file, create a write channel, write our data, and release the channel
  	err = qio_file_open_tmp(&f, 0, NULL);
        assert(!err);
      	err = qio_channel_create(&writing, f, QIO_CH_BUFFERED, 0, 1, 0, INT64_MAX, NULL);
        assert(!err);
      	err = qio_channel_write_amt(true, writing, "\xDE\xAD\xBE\xEF", 4);
        assert(!err);
        qio_channel_release(writing);

	//open a read channel to the tmp file, and read back the data.
    	err = qio_channel_create(&reading, f, QIO_CH_BUFFERED, 1, 0, 0, INT64_MAX, NULL);
        assert(!err);
      	err = qio_channel_read_amt(true, reading, buf, 4);
        assert(!err);
        qio_channel_release(reading);

	///check that what we wrote is what we read back
	if(memcmp(buf, "\xDE\xAD\xBE\xEF", 4) != 0){ assert(0);}

	//close the file
	qio_file_release(f);
	f = NULL;

	printf("PASS: test_verybasic\n");
}
Beispiel #2
0
void check_bits(int offset, int padding)
{
  qio_file_t* f;
  qio_channel_t* writing;
  qio_channel_t* reading;
  // bitpats is in pairs of data, num bits
  uint64_t bitpats[] = {1,1, 0,1, 1,1, 1,1, // 1011 -> b
                        0,1, 0,1, 1,1, 0,1, // 0010 -> 2
                        2,2, 3,2,           // 1011 -> b
                        2,2, 0,2,           // 1000 -> 8
                        0xffffffffffffffffull,64, // ffff ffff ffff ffff
                        0,6, 0x3f,6,        // 0000 0011 1111 -> 03f
                        0,1, 0xff,8, 0,1, 0x7f,7, 3,3,// 0111 1111 1011 1111 1011 ->7fbfb
                        0,3, 1,2, 0,3,      // 0000 1000 -> 08
                        1,1, 0x8100ff00ff20ff81ull,64, 0,7, // 1100 0000 1000 0000 0111 1111 1000 0000 0111 1111 1001 0000 0111 1111 1100 0000 1000 0000 -> c0807f807f907fc080
                        1,2
                       };

  uint8_t expect[] = {0xb2, 0xb8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0xf7, 0xfb, 0xfb, 0x08, 0xc0, 0x80, 0x7f, 0x80, 0x7f, 0x90, 0x7f, 0xc0, 0x80, 0x40, 0xff, 0x40, 0xff, 0x40};
  qioerr err;
  int i;


  if( verbose ) printf("\nBIT IO TEST AT OFFSET %i PADDING %i\n\n", offset, padding);

  err = qio_file_open_tmp(&f, 0, NULL);
  assert(!err);

  err = qio_channel_create(&writing, f, 0, 0, 1, 0, INT64_MAX, NULL);
  assert(!err);

  for( i = 0; i < offset; i++ ) {
    err = qio_channel_write_byte(true, writing, 0xc1);
    assert(!err);
  }

  // Write stuff to the file.
  for( i = 0; i < sizeof(bitpats)/(2*sizeof(uint64_t)); i++ ) {
    if( verbose ) printf("Writing %i bits of %llx\n", (int) bitpats[2*i + 1], (long long int) bitpats[2*i]);
    err = qio_channel_write_bits(true, writing, bitpats[2*i], bitpats[2*i + 1]);
    assert(!err);
  }
  if( verbose ) printf("Writing byte 0xff at the end\n");
  err = qio_channel_write_byte(true, writing, 0xff);
  assert(!err);
  if( verbose ) printf("Writing bits 010 at the end\n");
  err = qio_channel_write_bits(true, writing, 2, 3);
  assert(!err);
  if( verbose ) printf("Writing byte 0xff at the end\n");
  err = qio_channel_write_byte(true, writing, 0xff);
  assert(!err);
  if( verbose ) printf("Writing bits 010 at the end\n");
  err = qio_channel_write_bits(true, writing, 2, 3);
  assert(!err);

  for( i = 0; i < padding; i++ ) {
    err = qio_channel_write_byte(true, writing, 0xc1);
    assert(!err);
  }


  qio_channel_release(writing);

  // Read the data a byte at a time.
  err = qio_channel_create(&reading, f, 0, 1, 0, 0, INT64_MAX, NULL);
  assert(!err);

  for( i = 0; i < offset; i++ ) {
    uint8_t got = 0;
    err = qio_channel_read_amt(true, reading, &got, 1);
    assert(!err);
    assert(got == 0xc1);
  }

  // Read stuff from the file.
  for( i = 0; i < sizeof(expect); i++ ) {
    uint8_t got = 0;
    err = qio_channel_read_amt(true, reading, &got, 1);
    assert(!err);
    if( verbose ) printf("Reading byte expecting %x got %x\n", (int) expect[i], (int) got);
    assert( got == expect[i] );
  }

  for( i = 0; i < padding; i++ ) {
    uint8_t got = 0;
    err = qio_channel_read_amt(true, reading, &got, 1);
    assert(!err);
    assert(got == 0xc1);
  }


  qio_channel_release(reading);

  // Read the data with the binary reader.
  err = qio_channel_create(&reading, f, 0, 1, 0, 0, INT64_MAX, NULL);
  assert(!err);

  for( i = 0; i < offset; i++ ) {
    uint8_t got = 0;
    err = qio_channel_read_amt(true, reading, &got, 1);
    assert(!err);
    assert(got == 0xc1);
  }

  // Read stuff from the file.
  for( i = 0; i < sizeof(bitpats)/(2*sizeof(uint64_t)); i++ ) {
    uint64_t got = 0;
    err = qio_channel_read_bits(true, reading, &got, bitpats[2*i + 1]);
    assert(!err);
    if( verbose ) printf("Reading %i bits expecting %llx got %llx\n", (int) bitpats[2*i + 1], (long long int) bitpats[2*i], (long long int) got);
    assert( got == bitpats[2*i] );
  }

  {
    uint8_t got = 0;
    uint64_t b = 0;

    got = 0;
    err = qio_channel_read_amt(true, reading, &got, 1);
    assert(!err);
    assert( got == 0xff );
  
    b = 0;
    err = qio_channel_read_bits(true, reading, &b, 3);
    assert(!err);
    assert( b == 2 );
 
    got = 0;
    err = qio_channel_read_amt(true, reading, &got, 1);
    assert(!err);
    assert( got == 0xff );
 
    b = 0;
    err = qio_channel_read_bits(true, reading, &b, 3);
    assert(!err);
    assert( b == 2 );
  }

  for( i = 0; i < padding; i++ ) {
    uint8_t got = 0;
    err = qio_channel_read_amt(true, reading, &got, 1);
    assert(!err);
    assert(got == 0xc1);
  }

  qio_channel_release(reading);

  // Close the file.
  qio_file_release(f);
}
void string_escape_tests()
{
	qio_style_t style = qio_style_default();
	qio_channel_t *reading;
	qio_channel_t *writing;
	qio_file_t *f = NULL;
	err_t err;
        const char* inputs[2][4] = { {"a \"\\b\x01", // original string
                                      "\"a \\\"\\\\b\x01\"", // simple encoding
                                      "\"a \\\"\\\\b\\x01\"", // chapel encoding
                                      "\"a \\\"\\\\b\\u0001\"", // JSON encoding
                                     },
                                     { NULL, NULL, NULL, NULL }
                                   };
	char buf[50] = {0};
        int i,j;

	style.binary=0;
	style.string_start = '"';
	style.string_end = '"';
	
	err = qio_file_open_tmp(&f, 0, NULL);
        assert(!err);
        
        for( i = 0; inputs[i][0]; i++ ) {
          const char* input = inputs[i][0];
          ssize_t input_len = strlen(input);
          for( j = 1; j < 4; j++ ) {
            const char* expect = inputs[i][j];
            ssize_t expect_len = strlen(expect);
            style.string_format = j;

            err = qio_channel_create(&writing, f, QIO_CH_BUFFERED, 0, 1, 0, INT64_MAX, &style);
            assert(!err);
            err = qio_channel_print_string(true, writing, input, input_len);	
            assert(!err);
            qio_channel_release(writing);

            err = qio_channel_create(&reading, f, QIO_CH_BUFFERED, 1, 0, 0, INT64_MAX, &style);
            assert(!err);
            err = qio_channel_read_amt(true, reading, buf, expect_len);
            assert(!err);
     
            qio_channel_release(reading);

            //printf("Got %s expect %s\n", buf, expect);
            assert( memcmp(buf, expect, expect_len) == 0 );

            // Check that we can read it in again.
            err = qio_channel_create(&reading, f, QIO_CH_BUFFERED, 1, 0, 0, INT64_MAX, &style);
            assert(!err);

            {
              const char* got = NULL;
              ssize_t got_len = 0;
              err = qio_channel_scan_string(true, reading, &got, &got_len, -1);
              assert(!err);

              //printf("Read back %s expect %s\n", got, input);
              assert( got_len == input_len );
              assert( memcmp(got, input, got_len) == 0 );

              free((void*) got);
            }
     
            qio_channel_release(reading);
          }
        }

        qio_file_release(f);

        printf("PASS: simple escape\n");
}
void test_endian(void)
{
  // We write (hex) 00 0102 03040506 0708091011121314
  // as big endian and little endian, and then we check
  // that the data is what we expected.
  
  err_t err;
  qio_file_t* f;
  qio_channel_t* writing;
  qio_channel_t* reading;
  int64_t offset;
  int len = 15;
  uint8_t n0 = 0;
  uint16_t n1 = 0x0102;
  uint32_t n2 = 0x03040506;
  uint64_t n3 = 0x0708091011121314LL;
  const char* expect_le = "\x00\x02\x01\x06\x05\x04\x03"
                          "\x14\x13\x12\x11\x10\x09\x08\x07";
  const char* expect_be = "\x00\x01\x02\x03\x04\x05\x06"
                          "\x07\x08\x09\x10\x11\x12\x13\x14";
  char got[16];
  int i;
  int b_order;
  const char* expect;

  printf("Testing endian functions\n");

  err = qio_file_open_tmp(&f, 0, NULL);
  assert(!err);

  for( i = 0; i < 2; i++ ) {
    if( i == 0 ) {
      b_order = QIO_BIG;
      expect = expect_be;
    } else {
      b_order = QIO_LITTLE;
      expect = expect_le;
    }

    // Create a "write to file" channel.
    err = qio_channel_create(&writing, f, QIO_CH_BUFFERED, 0, 1, 0, INT64_MAX, NULL);
    assert(!err);

    err = qio_channel_write_uint8(true, writing, n0);
    assert(!err);
    err = qio_channel_write_uint16(true, b_order, writing, n1);
    assert(!err);
    err = qio_channel_write_uint32(true, b_order, writing, n2);
    assert(!err);
    err = qio_channel_write_uint64(true, b_order, writing, n3);
    assert(!err);

    err = qio_channel_offset(true, writing, &offset);
    assert(!err);

    assert( offset == len );

    qio_channel_release(writing);
    writing = NULL;

    // Create a "read from file" channel.
    err = qio_channel_create(&reading, f, QIO_CH_BUFFERED, 1, 0, 0, INT64_MAX, NULL);
    assert(!err);

    err = qio_channel_read_amt(true, reading, got, len);
    assert(!err);

    // Check that we read back what we wrote.
    assert( 0 == memcmp(got, expect, len) );

    qio_channel_release(reading);
    reading = NULL;
  }

  // Close the file.
  qio_file_release(f);
  f = NULL;


}
void test_quoted_string_maxlength(void)
{
  qio_style_t style = qio_style_default();
  qio_channel_t *reading;
  qio_channel_t *writing;
  qio_file_t *f = NULL;
  err_t err;
  const char* inputs[8][16] = {
    { "",     // original string
      "\"\"", // maxlen = 1
      "\"\"", // maxlen = 2
      "\"\"", // maxlen = 3
      "\"\"", // maxlen = 4
      "\"\"", // maxlen = 5
      "\"\"", // maxlen = 6
      "\"\"", // maxlen = 7
      "\"\"", // maxlen = 8
      "\"\"", // maxlen = 9
      "\"\"", // maxlen = 10
      "\"\"", // maxlen = 11
      "\"\"", // maxlen = 12
      "\"\"", // maxlen = 13
      "\"\"", // maxlen = 14
      NULL
    },
    { "a",     // original string
      "\"a\"", // maxlen = 1
      "\"a\"", // maxlen = 2
      "\"a\"", // maxlen = 3
      "\"a\"", // maxlen = 4
      "\"a\"", // maxlen = 5
      "\"a\"", // maxlen = 6
      "\"a\"", // maxlen = 7
      "\"a\"", // maxlen = 8
      "\"a\"", // maxlen = 9
      "\"a\"", // maxlen = 10
      "\"a\"", // maxlen = 11
      "\"a\"", // maxlen = 12
      "\"a\"", // maxlen = 13
      "\"a\"", // maxlen = 14
      NULL
    },
    { "ab",     // original string
      "\"ab\"", // maxlen = 1
      "\"ab\"", // maxlen = 2
      "\"ab\"", // maxlen = 3
      "\"ab\"", // maxlen = 4
      "\"ab\"", // maxlen = 5
      "\"ab\"", // maxlen = 6
      "\"ab\"", // maxlen = 7
      "\"ab\"", // maxlen = 8
      "\"ab\"", // maxlen = 9
      "\"ab\"", // maxlen = 10
      "\"ab\"", // maxlen = 11
      "\"ab\"", // maxlen = 12
      "\"ab\"", // maxlen = 13
      "\"ab\"", // maxlen = 14
      NULL
    },
    { "abc",     // original string
      "\"abc\"", // maxlen = 1
      "\"abc\"", // maxlen = 2
      "\"abc\"", // maxlen = 3
      "\"abc\"", // maxlen = 4
      "\"abc\"", // maxlen = 5
      "\"abc\"", // maxlen = 6
      "\"abc\"", // maxlen = 7
      "\"abc\"", // maxlen = 8
      "\"abc\"", // maxlen = 9
      "\"abc\"", // maxlen = 10
      "\"abc\"", // maxlen = 11
      "\"abc\"", // maxlen = 12
      "\"abc\"", // maxlen = 13
      "\"abc\"", // maxlen = 14
      NULL
    },
    { "abcd",     // original string
      "\"\"...", // maxlen = 1
      "\"\"...", // maxlen = 2
      "\"\"...", // maxlen = 3
      "\"\"...", // maxlen = 4
      "\"\"...", // maxlen = 5
      "\"abcd\"", // maxlen = 6
      "\"abcd\"", // maxlen = 7
      "\"abcd\"", // maxlen = 8
      "\"abcd\"", // maxlen = 9
      "\"abcd\"", // maxlen = 10
      "\"abcd\"", // maxlen = 11
      "\"abcd\"", // maxlen = 12
      "\"abcd\"", // maxlen = 13
      "\"abcd\"", // maxlen = 14
      NULL
    },

    { "123456789",     // original string
      "\"\"...",       // maxlen = 1
      "\"\"...",       // maxlen = 2
      "\"\"...",       // maxlen = 3
      "\"\"...",       // maxlen = 4
      "\"\"...",       // maxlen = 5
      "\"1\"...",      // maxlen = 6
      "\"12\"...",     // maxlen = 7
      "\"123\"...",    // maxlen = 8
      "\"1234\"...",   // maxlen = 9
      "\"12345\"...",  // maxlen = 10
      "\"123456789\"", // maxlen = 11
      "\"123456789\"", // maxlen = 12
      "\"123456789\"", // maxlen = 13
      "\"123456789\"", // maxlen = 14
      NULL
    },

    { "\x01X\x01Y", // original string
      "\"\"...",       // maxlen = 1
      "\"\"...",       // maxlen = 2
      "\"\"...",       // maxlen = 3
      "\"\"...",       // maxlen = 4
      "\"\"...",       // maxlen = 5
      "\"\"...",       // maxlen = 6
      "\"\"...",       // maxlen = 7
      "\"\"...",       // maxlen = 8
      "\"\\x01\"...",  // maxlen = 9
      "\"\\x01X\"...", // maxlen = 10
      "\"\\x01X\"...", // maxlen = 11
      "\"\\x01X\\x01Y\"", // maxlen = 12
      "\"\\x01X\\x01Y\"", // maxlen = 13
      "\"\\x01X\\x01Y\"", // maxlen = 14
      NULL
    },
    { NULL, NULL, NULL, NULL,
      NULL, NULL, NULL, NULL,
      NULL, NULL, NULL, NULL,
      NULL, NULL, NULL, NULL,
    }
  };

  char buf[50] = {0};
  int i,j;

  style.binary=0;
  style.string_start = '"';
  style.string_end = '"';
  
  err = qio_file_open_tmp(&f, 0, NULL);
  assert(!err);

  for( i = 0; inputs[i][0]; i++ ) {
    const char* input = inputs[i][0];
    ssize_t input_len = strlen(input);
    for( j = 1; inputs[i][j]; j++ ) {
      const char* expect = inputs[i][j];
      ssize_t expect_len = strlen(expect);
      qio_truncate_info_t ti;
      const char* got = NULL;
      ssize_t truncate_len = (j<input_len)?(j):(input_len);
      style.string_format = QIO_STRING_FORMAT_CHPL;
      style.max_width_columns = j;

      ti.max_columns = style.max_width_columns;
      ti.max_chars = SSIZE_MAX;
      ti.max_bytes = SSIZE_MAX;
      
      // check that qio_quote_string gives correct string when
      // used in a no-quote mode
      if( i != 6 ) {
        // but not for test 6 since the control characters are weird.
        err = qio_quote_string(0, 0, QIO_STRING_FORMAT_WORD, input, input_len, &got, &ti);
        assert(!err);
        assert(ti.ret_columns == truncate_len);
        assert(ti.ret_chars == truncate_len);
        assert(truncate_len == strlen(got));
        assert( 0 == memcmp(got, input, truncate_len) );
        free((void*) got);
      }

      // Now, check that qio_quote_string returns the correct string.
      err = qio_quote_string(style.string_start, style.string_end, style.string_format, input, input_len, &got, &ti);
      assert(!err);

      assert(ti.ret_columns == strlen(inputs[i][1]) || ti.ret_columns <= j);
      assert(ti.ret_bytes == expect_len);
      assert(ti.ret_bytes == strlen(got));
      assert( 0 == strcmp(got, expect) );
      free((void*) got);

      // Now, check that the quoting works correctly when writing.

      err = qio_channel_create(&writing, f, QIO_CH_BUFFERED, 0, 1, 0, INT64_MAX, &style);
      assert(!err);
      err = qio_channel_print_string(true, writing, input, input_len);	
      assert(!err);
      qio_channel_release(writing);

      err = qio_channel_create(&reading, f, QIO_CH_BUFFERED, 1, 0, 0, INT64_MAX, &style);
      assert(!err);
      err = qio_channel_read_amt(true, reading, buf, expect_len);
      assert(!err);

      qio_channel_release(reading);

      //printf("Got %s expect %s\n", buf, expect);
      assert( memcmp(buf, expect, expect_len) == 0 );
    }
  }

  qio_file_release(f);

  printf("PASS: quoted max length\n");
}