Exemple #1
0
void RingBuffer_test(void)
{
    // Return Value
    int rv;
    // External buffer to register as Ring Buffer
    char ext_buffer[20] = {0};
    // Temporary buffers for test purposes
    char read_buffer[40] = {0};
    char write_buffer[40] = {0};

    // Initialize Ring Buffer module
    rv = RingBuffer_init(ext_buffer, sizeof(ext_buffer));
    if (rv != RING_BUFFER_E_SUCCESS)
    {
        // TODO: Complete this statement
    }

    // Write 20 bytes to the Temporary Write buffer
    strcat(write_buffer, "Test Ring Buffer 20");

    // Write 10 bytes to the Ring Buffer
    rv = RingBuffer_write(write_buffer, 10);
    if (rv != 10)
    {
        // TODO: Complete this statement
    }

    // Read-back 5 first bytes from the Ring buffer
    rv = RingBuffer_read(read_buffer, 5);
    if (rv != 5)
    {
        // TODO: Complete this statement
    }

    // Read-back 5 last bytes from the Ring Buffer
    rv = RingBuffer_read(read_buffer + 5, 5);
    if (rv != 5)
    {
        // TODO: Complete this statement
    }

    // Compare the read byte with initial written data
    rv = memcmp(write_buffer, read_buffer, 10);
    if (rv != 0)
    {
        // Buffers are different, test FAILED
        // TODO: Complete this statement
    }
    else
    {
        // Buffers are identical, test PASSED
        // TODO: Complete this statement
    }
}
char *test_read()
{
	int rc = RingBuffer_read(buffer, data, blength(bss2));
	mu_assert(rc == blength(bss2), "Failed to read right amount of bytes.");
	mu_assert(strcmp(data, (char *)bss2->data) == 0, "Retrieved bss2 string is not the same.");

	rc = RingBuffer_read(buffer, data, blength(bss3));
	mu_assert(rc == blength(bss3), "Failed to read right amount of bytes.");
	mu_assert(strcmp(data, (char *)bss3->data) == 0, "Retrieved bss3 string is not the same.");

	// trying to read again should not work
	rc = RingBuffer_read(buffer, data, blength(bss3));
	mu_assert(rc == -1, "Failed to read right amount of bytes.");

	return NULL;
}
char *test_read_write()
{
  RingBuffer_puts(rbuf, &test_data_0);
  mu_assert(rbuf->end == 6, "ends at not reset correctly");
  mu_assert(RingBuffer_empty(rbuf) == 0, "not actually empty");

  RingBuffer_puts(rbuf, &test_data_1);
  mu_assert(rbuf->end == 34, "ends at not reset correctly");

  char *data_1 = malloc(sizeof(char) * 6);
  RingBuffer_read(rbuf, data_1, 6);
  mu_assert(bstrcmp(bfromcstr(data_1), &test_data_0) == 0,
            "read did not return proper string"
           );
  mu_assert(rbuf->start == 6, "did not reset start correctly");

  char *data_2 = malloc(sizeof(char) * 28);
  RingBuffer_read(rbuf, data_2, 28);
  mu_assert(bstrcmp(bfromcstr(data_2), &test_data_1) == 0,
            "read did not return proper string"
           );
  mu_assert(rbuf->start == 0, "did not reset start correctly");
  mu_assert(rbuf->end == 0, "did not reset end correctly");


  RingBuffer_puts(rbuf, &test_data_2);
  RingBuffer_puts(rbuf, &test_data_3);
  mu_assert(rbuf->start == 0, "start not maintained");
  mu_assert(rbuf->end == 64, "end not set correctly");
  mu_assert(RingBuffer_full(rbuf) == 1, "RingBuffer actually full");
  mu_assert(RingBuffer_available_space(rbuf) == 0, "RingBuffer has no space");
  mu_assert(RingBuffer_available_data(rbuf) == 64, "RingBuffer has wrong available data");

  bstring data_3 = RingBuffer_get_all(rbuf);
  bstring expected = bformat("%s%s", bdata(&test_data_2), bdata(&test_data_3));
  mu_assert(bstrcmp(data_3, expected) == 0, "data not returned correctly");
  mu_assert(RingBuffer_empty(rbuf) == 1, "RingBuffer should be empty");

  return NULL;
}