Sun, 24 Apr 2022 15:15:39 +0200
#170 first buffer tests
src/buffer.c | file | annotate | diff | comparison | revisions | |
src/cx/buffer.h | file | annotate | diff | comparison | revisions | |
test/CMakeLists.txt | file | annotate | diff | comparison | revisions | |
test/test_buffer.cpp | file | annotate | diff | comparison | revisions |
1.1 --- a/src/buffer.c Sun Apr 24 14:54:50 2022 +0200 1.2 +++ b/src/buffer.c Sun Apr 24 15:15:39 2022 +0200 1.3 @@ -30,6 +30,7 @@ 1.4 #include "cx/utils.h" 1.5 1.6 #include <stdlib.h> 1.7 +#include <stdio.h> 1.8 #include <string.h> 1.9 1.10 int cxBufferInit(
2.1 --- a/src/cx/buffer.h Sun Apr 24 14:54:50 2022 +0200 2.2 +++ b/src/cx/buffer.h Sun Apr 24 15:15:39 2022 +0200 2.3 @@ -49,7 +49,6 @@ 2.4 2.5 #include "common.h" 2.6 #include "allocator.h" 2.7 -#include <stdio.h> 2.8 2.9 #ifdef __cplusplus 2.10 extern "C" {
3.1 --- a/test/CMakeLists.txt Sun Apr 24 14:54:50 2022 +0200 3.2 +++ b/test/CMakeLists.txt Sun Apr 24 15:15:39 2022 +0200 3.3 @@ -15,6 +15,7 @@ 3.4 3.5 add_executable(ucxtest 3.6 test_allocator.cpp 3.7 + test_buffer.cpp 3.8 test_list.cpp 3.9 test_tree.cpp 3.10 selftest.cpp
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/test_buffer.cpp Sun Apr 24 15:15:39 2022 +0200 4.3 @@ -0,0 +1,74 @@ 4.4 +/* 4.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 4.6 + * 4.7 + * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. 4.8 + * 4.9 + * Redistribution and use in source and binary forms, with or without 4.10 + * modification, are permitted provided that the following conditions are met: 4.11 + * 4.12 + * 1. Redistributions of source code must retain the above copyright 4.13 + * notice, this list of conditions and the following disclaimer. 4.14 + * 4.15 + * 2. Redistributions in binary form must reproduce the above copyright 4.16 + * notice, this list of conditions and the following disclaimer in the 4.17 + * documentation and/or other materials provided with the distribution. 4.18 + * 4.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 4.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 4.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 4.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 4.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 4.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 4.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 4.29 + * POSSIBILITY OF SUCH DAMAGE. 4.30 + */ 4.31 + 4.32 +#include "cx/buffer.h" 4.33 + 4.34 +#include <gtest/gtest.h> 4.35 +#include "util_allocator.h" 4.36 + 4.37 +class Buffer : public ::testing::Test { 4.38 +protected: 4.39 + void TearDown() override { 4.40 + EXPECT_TRUE(testingAllocator.verify()); 4.41 + } 4.42 + 4.43 + CxTestingAllocator testingAllocator; 4.44 +}; 4.45 + 4.46 +TEST_F(Buffer, WrapSpace) { 4.47 + CxBuffer buf; 4.48 + void *space = cxMalloc(&testingAllocator, 16); 4.49 + cxBufferInit(&buf, space, 16, &testingAllocator, CX_BUFFER_DEFAULT); 4.50 + EXPECT_EQ(buf.space, space); 4.51 + EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0); 4.52 + EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, 0); 4.53 + EXPECT_EQ(buf.pos, 0); 4.54 + EXPECT_EQ(buf.size, 0); 4.55 + EXPECT_EQ(buf.capacity, 16); 4.56 + EXPECT_EQ(buf.allocator, &testingAllocator); 4.57 + cxBufferDestroy(&buf); 4.58 + EXPECT_FALSE(testingAllocator.verify()); 4.59 + cxFree(&testingAllocator, space); 4.60 + EXPECT_TRUE(testingAllocator.verify()); 4.61 +} 4.62 + 4.63 +TEST_F(Buffer, WrapSpaceAutoFree) { 4.64 + CxBuffer buf; 4.65 + void *space = cxMalloc(&testingAllocator, 16); 4.66 + cxBufferInit(&buf, space, 16, &testingAllocator, CX_BUFFER_FREE_CONTENTS); 4.67 + EXPECT_EQ(buf.space, space); 4.68 + EXPECT_EQ(buf.flags & CX_BUFFER_AUTO_EXTEND, 0); 4.69 + EXPECT_EQ(buf.flags & CX_BUFFER_FREE_CONTENTS, CX_BUFFER_FREE_CONTENTS); 4.70 + EXPECT_EQ(buf.pos, 0); 4.71 + EXPECT_EQ(buf.size, 0); 4.72 + EXPECT_EQ(buf.capacity, 16); 4.73 + EXPECT_EQ(buf.allocator, &testingAllocator); 4.74 + EXPECT_FALSE(testingAllocator.verify()); 4.75 + cxBufferDestroy(&buf); 4.76 + EXPECT_TRUE(testingAllocator.verify()); 4.77 +}