test/buffer_tests.c

changeset 290
d5d6ab809ad3
parent 269
591473851c95
     1.1 --- a/test/buffer_tests.c	Wed May 09 15:04:15 2018 +0200
     1.2 +++ b/test/buffer_tests.c	Wed May 09 20:15:10 2018 +0200
     1.3 @@ -625,3 +625,114 @@
     1.4      
     1.5      ucx_buffer_free(b);
     1.6  }
     1.7 +
     1.8 +UCX_TEST(test_ucx_buffer_shl) {
     1.9 +    
    1.10 +    const char* hw = "Shift the World!";
    1.11 +    
    1.12 +    UcxBuffer *b = ucx_buffer_new(NULL, 20, UCX_BUFFER_DEFAULT);
    1.13 +    ucx_buffer_puts(b, hw);
    1.14 +    b->pos = 5;
    1.15 +    
    1.16 +    UCX_TEST_BEGIN
    1.17 +    char* expected;
    1.18 +    
    1.19 +    ucx_buffer_shift_left(b, 2);
    1.20 +    expected = "ift the World!";
    1.21 +    
    1.22 +    UCX_TEST_ASSERT(b->pos == 3, "position after normal shl wrong");
    1.23 +    UCX_TEST_ASSERT(b->size == strlen(expected), "size after normal shl wrong");
    1.24 +    UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size),
    1.25 +            "contents after normal shl wrong");
    1.26 +    
    1.27 +    
    1.28 +    ucx_buffer_shift_left(b, 5);
    1.29 +    expected = "he World!";
    1.30 +    
    1.31 +    UCX_TEST_ASSERT(b->pos == 0, "position after overshift left wrong");
    1.32 +    UCX_TEST_ASSERT(b->size == strlen(expected),
    1.33 +            "size after overshift left wrong");
    1.34 +    UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size),
    1.35 +            "contents after overshift left wrong");
    1.36 +    
    1.37 +    ucx_buffer_shift_left(b, 10);
    1.38 +    UCX_TEST_ASSERT(b->pos == 0, "position after 'shl everything away' wrong");
    1.39 +    UCX_TEST_ASSERT(b->size == 0, "size after 'shl everything away' wrong");
    1.40 +    
    1.41 +    UCX_TEST_END
    1.42 +    
    1.43 +    ucx_buffer_free(b);    
    1.44 +}
    1.45 +
    1.46 +UCX_TEST(test_ucx_buffer_shr) {
    1.47 +    
    1.48 +    const char* hw = "Shift the World!";
    1.49 +    
    1.50 +    UcxBuffer *b = ucx_buffer_new(NULL, 20, UCX_BUFFER_DEFAULT);
    1.51 +    ucx_buffer_puts(b, hw);
    1.52 +    b->pos = 12;
    1.53 +    
    1.54 +    UCX_TEST_BEGIN
    1.55 +    char* expected;
    1.56 +    
    1.57 +    ucx_buffer_shift_right(b, 2);
    1.58 +    expected = "ShShift the World!";
    1.59 +    
    1.60 +    UCX_TEST_ASSERT(b->pos == 14, "position after normal shr wrong");
    1.61 +    UCX_TEST_ASSERT(b->size == strlen(expected), "size after normal shr wrong");
    1.62 +    UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size),
    1.63 +            "contents after normal shr wrong");
    1.64 +    
    1.65 +    
    1.66 +    ucx_buffer_shift_right(b, 5);
    1.67 +    expected = "ShShiShShift the Wor";
    1.68 +    UCX_TEST_ASSERT(strlen(expected) == b->capacity,
    1.69 +            "Test data is wrong, please fix the test.");
    1.70 +    
    1.71 +    UCX_TEST_ASSERT(b->pos == 19,
    1.72 +            "position after overshift right w/o auto-extend wrong");
    1.73 +    UCX_TEST_ASSERT(b->size == 20,
    1.74 +            "size after overshift right w/o auto-extend wrong");
    1.75 +    UCX_TEST_ASSERT(b->capacity == 20,
    1.76 +            "capacity after overshift right w/o auto-extend wrong");
    1.77 +    UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size),
    1.78 +            "contents after overshift right w/o auto-extend wrong");
    1.79 +    
    1.80 +    ucx_buffer_shift_right(b, 15);
    1.81 +    UCX_TEST_ASSERT(b->pos == b->capacity, "position after 'shr to eof' wrong");
    1.82 +    UCX_TEST_ASSERT(b->size == b->capacity, "size after 'shr to eof' wrong");
    1.83 +    UCX_TEST_ASSERT(ucx_buffer_eof(b), "buffer not eof after 'shr to eof'");
    1.84 +    
    1.85 +    UCX_TEST_END
    1.86 +    
    1.87 +    ucx_buffer_free(b);    
    1.88 +}
    1.89 +
    1.90 +UCX_TEST(test_ucx_buffer_shr_ax) {
    1.91 +    
    1.92 +    const char* hw = "Shift the World!";
    1.93 +    
    1.94 +    UcxBuffer *b = ucx_buffer_new(NULL, 20, UCX_BUFFER_AUTOEXTEND);
    1.95 +    ucx_buffer_puts(b, hw);
    1.96 +    b->pos = 12;
    1.97 +    
    1.98 +    UCX_TEST_BEGIN
    1.99 +    
   1.100 +    const char* expected = "Shift the Shift the World!";
   1.101 +    
   1.102 +    ucx_buffer_shift_right(b, 10);
   1.103 +    UCX_TEST_ASSERT(b->pos == 22, "position after shr w/ auto-extend wrong");
   1.104 +    UCX_TEST_ASSERT(b->size == strlen(expected),
   1.105 +            "size after shr w/ auto-extend wrong");
   1.106 +    UCX_TEST_ASSERT(b->capacity >= b->size,
   1.107 +            "auto-extension of capacity after shr w/ auto-extend failed");
   1.108 +    UCX_TEST_ASSERT(!ucx_buffer_eof(b),
   1.109 +            "buffer should not be eof after shr w/ auto-extend");
   1.110 +    UCX_TEST_ASSERT(!memcmp(b->space, expected, b->size),
   1.111 +            "contents wrong after shr w/ auto-extend");
   1.112 +    
   1.113 +    UCX_TEST_END
   1.114 +    
   1.115 +    ucx_buffer_free(b);    
   1.116 +}
   1.117 +

mercurial