test/utils_tests.c

Wed, 14 Aug 2013 15:22:35 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 14 Aug 2013 15:22:35 +0200
changeset 142
ee8cb27d8b8e
child 145
e974640ec4e0
permissions
-rw-r--r--

added printf functions

olaf@142 1 /*
olaf@142 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
olaf@142 3 *
olaf@142 4 * Copyright 2013 Olaf Wintermann. All rights reserved.
olaf@142 5 *
olaf@142 6 * Redistribution and use in source and binary forms, with or without
olaf@142 7 * modification, are permitted provided that the following conditions are met:
olaf@142 8 *
olaf@142 9 * 1. Redistributions of source code must retain the above copyright
olaf@142 10 * notice, this list of conditions and the following disclaimer.
olaf@142 11 *
olaf@142 12 * 2. Redistributions in binary form must reproduce the above copyright
olaf@142 13 * notice, this list of conditions and the following disclaimer in the
olaf@142 14 * documentation and/or other materials provided with the distribution.
olaf@142 15 *
olaf@142 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
olaf@142 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
olaf@142 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
olaf@142 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
olaf@142 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
olaf@142 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
olaf@142 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
olaf@142 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
olaf@142 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
olaf@142 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
olaf@142 26 * POSSIBILITY OF SUCH DAMAGE.
olaf@142 27 */
olaf@142 28
olaf@142 29 #include "utils_tests.h"
olaf@142 30 #include "ucx/buffer.h"
olaf@142 31
olaf@142 32 UCX_TEST(test_ucx_fprintf) {
olaf@142 33 UcxBuffer *b1 = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND);
olaf@142 34 UcxBuffer *b2 = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
olaf@142 35
olaf@142 36 char *teststr1 = (char*)calloc(1, 1024);
olaf@142 37 char *teststr2 = (char*)calloc(1, 1024);
olaf@142 38 memset(teststr1, 'a', 1023);
olaf@142 39 memset(teststr2, 'b', 1023);
olaf@142 40
olaf@142 41 UCX_TEST_BEGIN
olaf@142 42
olaf@142 43 ucx_fprintf(b1, (write_func)ucx_buffer_write, "Hello %s", "World");
olaf@142 44 UCX_TEST_ASSERT(!strcmp(b1->space, "Hello World"), "wrong content in b1");
olaf@142 45 ucx_fprintf(b1, (write_func)ucx_buffer_write, "\nend.\n");
olaf@142 46 UCX_TEST_ASSERT(!strcmp(b1->space, "Hello World\nend.\n"),
olaf@142 47 "wrong content in b1 after second fprintf");
olaf@142 48
olaf@142 49 ucx_fprintf(b2, (write_func)ucx_buffer_write, "%s%s", teststr1, teststr2);
olaf@142 50 UCX_TEST_ASSERT(!memcmp(b2->space, teststr1, 1023),
olaf@142 51 "wrong first half in b2");
olaf@142 52 UCX_TEST_ASSERT(!memcmp(b2->space+1023, teststr2, 1023),
olaf@142 53 "wrong second half in b2");
olaf@142 54
olaf@142 55 UCX_TEST_END
olaf@142 56 }
olaf@142 57
olaf@142 58 UCX_TEST(test_ucx_asprintf) {
olaf@142 59 char *teststr1 = (char*)calloc(1, 1024);
olaf@142 60 char *teststr2 = (char*)calloc(1, 1024);
olaf@142 61 memset(teststr1, 'a', 1023);
olaf@142 62 memset(teststr2, 'b', 1023);
olaf@142 63 UcxAllocator *a = ucx_default_allocator();
olaf@142 64
olaf@142 65 UCX_TEST_BEGIN
olaf@142 66
olaf@142 67 sstr_t s1 = ucx_asprintf(a, "int: %d\nHello %s!", 123, "World");
olaf@142 68 UCX_TEST_ASSERT(s1.ptr, "s1.ptr is NULL");
olaf@142 69 UCX_TEST_ASSERT(s1.length == 21, "wrong length");
olaf@142 70 UCX_TEST_ASSERT(!sstrcmp(s1, S("int: 123\nHello World!")), "wrong content");
olaf@142 71 free(s1.ptr);
olaf@142 72
olaf@142 73 sstr_t s2 = ucx_asprintf(a, "%s%s", teststr1, teststr2);
olaf@142 74 UCX_TEST_ASSERT(!memcmp(s2.ptr, teststr1, 1023),
olaf@142 75 "wrong first half in s2");
olaf@142 76 UCX_TEST_ASSERT(!memcmp(s2.ptr+1023, teststr2, 1023),
olaf@142 77 "wrong second half in s2");
olaf@142 78
olaf@142 79 UCX_TEST_END
olaf@142 80 }

mercurial