예제 #1
0
static void brigade_pread_simple(void **state)
{
  sln_brigade_t *bb;
  sln_bucket_t *e;
  char buf[5];
  size_t len = 0;

  SLN_ERR(sln_brigade_create(&bb));
  SLN_ERR(sln_bucket_create_empty(&e, 20));
  SLN_BRIGADE_INSERT_TAIL(bb, e);
  memset(e->data, 'B', e->size);
  memset(e->data, 'A', 1);

  SLN_ERR(sln_brigade_pread_bytes(bb, 0, 1, &buf[0], &len));
  assert_int_equal(len, 1);
  assert_memory_equal(buf, "A", 1);

  SLN_ERR(sln_brigade_pread_bytes(bb, 1, 1, &buf[0], &len));
  assert_int_equal(len, 1);
  assert_memory_equal(buf, "B", 1);

  SLN_ERR(sln_brigade_pread_bytes(bb, 0, 2, &buf[0], &len));
  assert_int_equal(len, 2);
  assert_memory_equal(buf, "AB", 2);

  SLN_ERR(sln_brigade_pread_bytes(bb, 2, 2, &buf[0], &len));
  assert_int_equal(len, 2);
  assert_memory_equal(buf, "BB", 2);

  sln_brigade_destroy(bb);
}
예제 #2
0
static void brigade_copy_into(void **state)
{
  sln_brigade_t *source;
  sln_brigade_t *dest;
  sln_bucket_t *e1;
  char buf[20];
  size_t len = 0;

  SLN_ERR(sln_brigade_create(sln_test_alloc, &source));
  SLN_ERR(sln_bucket_create_empty(sln_test_alloc, &e1, 10));
  SLN_BRIGADE_INSERT_TAIL(source, e1);
  memset(e1->data, 'A', e1->size);

  SLN_ERR(sln_brigade_create(sln_test_alloc, &dest));
  SLN_ERR(sln_brigade_copy_into(source, 0, 10, dest));
  assert_int_equal(sln_brigade_size(dest), 10);
  SLN_ERR(sln_brigade_pread_bytes(dest, 0, 10, &buf[0], &len));
  assert_memory_equal(buf, "AAAAAAAAAA", 2);
  sln_brigade_clear(dest);
  sln_brigade_destroy(dest);

  SLN_ERR(sln_brigade_create(sln_test_alloc, &dest));
  SLN_ERR(sln_brigade_copy_into(source, 2, 4, dest));
  assert_int_equal(sln_brigade_size(dest), 4);
  SLN_ERR(sln_brigade_pread_bytes(dest, 0, 4, &buf[0], &len));
  assert_memory_equal(buf, "AAAA", 4);
  sln_brigade_clear(dest);
  sln_brigade_destroy(dest);

  sln_brigade_destroy(source);
}
예제 #3
0
static void brigade_chomp(void **state) {
  sln_brigade_t *bb;
  sln_bucket_t *e1;
  char buf[20];
  size_t len = 0;

  SLN_ERR(sln_brigade_create(sln_test_alloc, &bb));
  SLN_ERR(sln_bucket_create_empty(sln_test_alloc, &e1, 10));
  SLN_BRIGADE_INSERT_TAIL(bb, e1);
  memset(e1->data, 'A', e1->size);
  assert_int_equal(sln_brigade_size(bb), 10);
  SLN_ERR(sln_brigade_chomp(bb, 1));
  assert_int_equal(sln_brigade_size(bb), 9);
  SLN_ERR(sln_brigade_chomp(bb, 8));
  assert_int_equal(sln_brigade_size(bb), 1);
  sln_brigade_destroy(bb);

  SLN_ERR(sln_brigade_create(sln_test_alloc, &bb));
  SLN_ERR(sln_bucket_create_empty(sln_test_alloc, &e1, 10));
  SLN_BRIGADE_INSERT_TAIL(bb, e1);
  memset(e1->data, 'A', e1->size);
  SLN_ERR(sln_bucket_create_empty(sln_test_alloc, &e1, 10));
  SLN_BRIGADE_INSERT_TAIL(bb, e1);
  memset(e1->data, 'B', e1->size);
  assert_int_equal(sln_brigade_size(bb), 20);

  SLN_ERR(sln_brigade_chomp(bb, 8));
  assert_int_equal(sln_brigade_size(bb), 12);
  SLN_ERR(sln_brigade_pread_bytes(bb, 0, 4, &buf[0], &len));
  assert_memory_equal(buf, "AABB", 4);

  SLN_ERR(sln_brigade_chomp(bb, 2));
  assert_int_equal(sln_brigade_size(bb), 10);
  SLN_ERR(sln_brigade_pread_bytes(bb, 0, 4, &buf[0], &len));
  assert_memory_equal(buf, "BBBB", 4);

  SLN_ERR(sln_brigade_chomp(bb, 2));
  SLN_ERR(sln_brigade_pread_bytes(bb, 0, 8, &buf[0], &len));
  assert_memory_equal(buf, "BBBBBBBB", 8);
  assert_int_equal(sln_brigade_size(bb), 8);
  sln_brigade_destroy(bb);
}
예제 #4
0
static void brigade_pread_more_buckets(void **state)
{
  sln_brigade_t *bb;
  sln_bucket_t *e1;
  sln_bucket_t *e2;
  char buf[20];
  size_t len = 0;

  SLN_ERR(sln_brigade_create(&bb));
  SLN_ERR(sln_bucket_create_empty(&e1, 10));
  SLN_BRIGADE_INSERT_TAIL(bb, e1);
  memset(e1->data, 'A', e1->size);

  SLN_ERR(sln_bucket_create_empty(&e2, 10));
  SLN_BRIGADE_INSERT_TAIL(bb, e2);
  memset(e2->data, 'B', e2->size);

  SLN_ERR(sln_brigade_pread_bytes(bb, 9, 1, &buf[0], &len));
  assert_int_equal(len, 1);
  assert_memory_equal(buf, "A", 1);

  SLN_ERR(sln_brigade_pread_bytes(bb, 11, 1, &buf[0], &len));
  assert_int_equal(len, 1);
  assert_memory_equal(buf, "B", 1);

  SLN_ERR(sln_brigade_pread_bytes(bb, 9, 2, &buf[0], &len));
  assert_int_equal(len, 2);
  assert_memory_equal(buf, "AB", 2);

  SLN_ERR(sln_brigade_pread_bytes(bb, 11, 2, &buf[0], &len));
  assert_int_equal(len, 2);
  assert_memory_equal(buf, "BB", 2);

  SLN_ERR(sln_brigade_pread_bytes(bb, 0, 20, &buf[0], &len));
  assert_int_equal(len, 20);
  assert_memory_equal(buf, "AAAAAAAAAABBBBBBBBBB", 20);

  sln_brigade_destroy(bb);
}
예제 #5
0
파일: tok.c 프로젝트: pquerna/selene
selene_error_t *sln_tok_parser(sln_brigade_t *bb, sln_tok_cb cb, void *baton) {
  int keepgoing = 1;
  selene_error_t *err = SELENE_SUCCESS;
  sln_tok_value_t tvalue;
  size_t rlen;
  size_t offset = 0;
  sln_brigade_t *tmpbb = NULL;

  memset(&tvalue, 0, sizeof(tvalue));

  tvalue.next = TOK_INIT;

  while (keepgoing == 1) {
    tvalue.current = tvalue.next;

    err = cb(&tvalue, baton);

    if (err) {
      break;
    }

    switch (tvalue.next) {
      case TOK__UNUSED:
      case TOK__MAX:
      case TOK_INIT:
      case TOK_DONE:
        keepgoing = 0;
        break;

      case TOK_SKIP:
        if (tvalue.wantlen > sln_brigade_size(bb)) {
          keepgoing = 0;
          break;
        }
        break;

      case TOK_UINT16:
        SLN_ASSERT(tvalue.wantlen == 2);

        err = sln_brigade_pread_bytes(bb, offset, tvalue.wantlen,
                                      &tvalue.v.bytes[0], &rlen);
        if (err) {
          keepgoing = 0;
          break;
        }

        if (rlen != tvalue.wantlen) {
          keepgoing = 0;
          break;
        }

        tvalue.v.uint16 = (((unsigned char)tvalue.v.bytes[0]) << 8 |
                           ((unsigned char)tvalue.v.bytes[1]));
        break;
      case TOK_UINT24:
        SLN_ASSERT(tvalue.wantlen == 3);

        err = sln_brigade_pread_bytes(bb, offset, tvalue.wantlen,
                                      &tvalue.v.bytes[0], &rlen);
        if (err) {
          keepgoing = 0;
          break;
        }

        if (rlen != tvalue.wantlen) {
          keepgoing = 0;
          break;
        }

        tvalue.v.uint24 = (((unsigned char)tvalue.v.bytes[0]) << 16 |
                           ((unsigned char)tvalue.v.bytes[1]) << 8 |
                           ((unsigned char)tvalue.v.bytes[2]));
        break;

      case TOK_COPY_BYTES:
        SLN_ASSERT(tvalue.wantlen <= SLN_TOK_VALUE_MAX_BYTE_COPY_LEN);

        err = sln_brigade_pread_bytes(bb, offset, tvalue.wantlen,
                                      &tvalue.v.bytes[0], &rlen);
        if (err) {
          keepgoing = 0;
          break;
        }
        if (rlen != tvalue.wantlen) {
          keepgoing = 0;
        }
        break;

      case TOK_COPY_BRIGADE:

        if (tvalue.wantlen > sln_brigade_size(bb)) {
          keepgoing = 0;
          break;
        }

        if (tmpbb == NULL) {
          err = sln_brigade_create(bb->alloc, &tmpbb);
          if (err) {
            keepgoing = 0;
            break;
          }
        } else {
          sln_brigade_clear(tmpbb);
        }

        tvalue.v.bb = tmpbb;
        /* TODO: optimization, this isn't required */
        err = sln_brigade_copy_into(bb, offset, tvalue.wantlen, tmpbb);
        if (err) {
          keepgoing = 0;
          break;
        }
        break;
    }
    offset += tvalue.wantlen;
    tvalue.current = TOK__UNUSED;
  }

  if (tmpbb != NULL) {
    sln_brigade_destroy(tmpbb);
  }

  return err;
}