Skip to content

Commit

Permalink
add test case for base utility Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
IronsDu committed Feb 15, 2020
1 parent 0d45740 commit 93ddf82
Show file tree
Hide file tree
Showing 16 changed files with 358 additions and 156 deletions.
12 changes: 6 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if(WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++latest")
elseif(UNIX)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_BUILD_TYPE "Release")
endif()
SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -ggdb -Wall -Wextra -D_DEBUG")
SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -g -ggdb -Wall -Wextra -DNDEBUG")
Expand All @@ -47,10 +47,10 @@ endif(brynet_BUILD_EXAMPLES)

if (brynet_BUILD_TESTS)
if(WIN32)
if (MSVC_VERSION VERSION_GREATER 1900)
#add_subdirectory(tests)
endif()
elseif(UNIX)
#add_subdirectory(tests)
if (MSVC_VERSION VERSION_GREATER 1900)
add_subdirectory(tests)
endif()
elseif(UNIX)
add_subdirectory(tests)
endif()
endif(brynet_BUILD_TESTS)
21 changes: 9 additions & 12 deletions include/brynet/base/AppStatus.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <stdbool.h>
#include <stdio.h>
#include <cstdbool>
#include <cstdio>
#include <signal.h>

#include <brynet/base/Platform.hpp>
Expand All @@ -16,34 +16,31 @@

namespace brynet { namespace base {

static int app_kbhit(void)
static bool app_kbhit()
{
#ifdef BRYNET_PLATFORM_WINDOWS
return _kbhit();
#else
struct termios oldt, newt;
int ch;
int oldf;

struct termios oldt;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
auto newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
const auto oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);

ch = getchar();
const auto ch = getchar();

tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);

if (ch != EOF)
{
ungetc(ch, stdin);
return 1;
return true;
}

return 0;
return false;
#endif
}

Expand Down
59 changes: 29 additions & 30 deletions include/brynet/base/Array.hpp
Original file line number Diff line number Diff line change
@@ -1,72 +1,75 @@
#pragma once

#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <cstdbool>
#include <cstring>
#include <cstdlib>
#include <cassert>

namespace brynet { namespace base {

struct array_s
{
char* buffer;
void* buffer;
size_t buffer_size;
size_t element_size;
size_t element_num;
};

static void array_delete(struct array_s* self)
{
if (self == NULL)
if (self == nullptr)
{
return;
}

if (self->buffer != NULL)
if (self->buffer != nullptr)
{
free(self->buffer);
self->buffer = NULL;
self->buffer = nullptr;
}

self->element_num = 0;
free(self);
self = NULL;
self = nullptr;
}

static struct array_s* array_new(size_t num, size_t element_size)
{
size_t buffer_size = num * element_size;
struct array_s* ret = (struct array_s*)malloc(sizeof(struct array_s));
if (ret == NULL)
auto ret = (struct array_s*)malloc(sizeof(struct array_s));
if (ret == nullptr)
{
return NULL;
return nullptr;
}

memset(ret, 0, sizeof(*ret));
ret->buffer = (char*)malloc(buffer_size);
if (ret->buffer != NULL)
const auto buffer_size = num * element_size;

ret->buffer_size = 0;
ret->element_size = 0;
ret->element_num = 0;
ret->buffer = malloc(buffer_size);

if (ret->buffer != nullptr)
{
memset(ret->buffer, 0, buffer_size);
ret->element_size = element_size;
ret->element_num = num;
ret->buffer_size = buffer_size;
}
else
{
array_delete(ret);
ret = NULL;
ret = nullptr;
}

return ret;
}

static char* array_at(struct array_s* self, size_t index)
static void* array_at(struct array_s* self, size_t index)
{
char* ret = NULL;
void* ret = nullptr;

if (index < self->element_num)
{
ret = self->buffer + (index * self->element_size);
ret = (char*)(self->buffer) + (index * self->element_size);
}
else
{
Expand All @@ -78,9 +81,9 @@ namespace brynet { namespace base {

static bool array_set(struct array_s* self, size_t index, const void* data)
{
char* old_data = array_at(self, index);
void* old_data = array_at(self, index);

if (old_data != NULL)
if (old_data != nullptr)
{
memcpy(old_data, data, self->element_size);
return true;
Expand All @@ -93,20 +96,16 @@ namespace brynet { namespace base {

static bool array_increase(struct array_s* self, size_t increase_num)
{
size_t new_buffer_size = 0;
char* new_buffer = NULL;

if (increase_num == 0)
{
return false;
}

new_buffer_size = self->buffer_size + increase_num * self->element_size;
new_buffer = (char*)malloc(new_buffer_size);
const auto new_buffer_size = self->buffer_size + increase_num * self->element_size;
auto new_buffer = malloc(new_buffer_size);

if (new_buffer != NULL)
if (new_buffer != nullptr)
{
memset(new_buffer, 0, new_buffer_size);
memcpy(new_buffer, self->buffer, self->buffer_size);
free(self->buffer);
self->buffer = new_buffer;
Expand Down
60 changes: 35 additions & 25 deletions include/brynet/base/Buffer.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <cstdbool>
#include <cstdint>
#include <cstdlib>
#include <cstring>

namespace brynet { namespace base {

Expand All @@ -18,39 +18,42 @@ namespace brynet { namespace base {

static void buffer_delete(struct buffer_s* self)
{
if (self == NULL)
if (self == nullptr)
{
return;
}

if (self->data != NULL)
if (self->data != nullptr)
{
free(self->data);
self->data = NULL;
self->data = nullptr;
}

free(self);
self = NULL;
self = nullptr;
}

static struct buffer_s* buffer_new(size_t buffer_size)
{
struct buffer_s* ret = (struct buffer_s*)malloc(sizeof(struct buffer_s));
if (ret == NULL)
if (ret == nullptr)
{
return NULL;
return nullptr;
}

if ((ret->data = (char*)malloc(sizeof(char) * buffer_size)) != NULL)
ret->data_len = 0;
ret->read_pos = 0;
ret->write_pos = 0;
ret->data = (char*)malloc(sizeof(char) * buffer_size);

if (ret->data != nullptr)
{
ret->data_len = buffer_size;
ret->read_pos = 0;
ret->write_pos = 0;
}
else
{
buffer_delete(ret);
ret = NULL;
ret = nullptr;
}

return ret;
Expand All @@ -63,14 +66,12 @@ namespace brynet { namespace base {

static void buffer_adjustto_head(struct buffer_s* self)
{
size_t len = 0;

if (self->read_pos <= 0)
if (self->read_pos == 0)
{
return;
}

len = buffer_getreadvalidcount(self);
const auto len = buffer_getreadvalidcount(self);
if (len > 0)
{
memmove(self->data, self->data + self->read_pos, len);
Expand All @@ -96,21 +97,31 @@ namespace brynet { namespace base {
return self->read_pos;
}

static void buffer_addwritepos(struct buffer_s* self, size_t value)
static bool buffer_addwritepos(struct buffer_s* self, size_t value)
{
size_t temp = self->write_pos + value;
const size_t temp = self->write_pos + value;
if (temp <= self->data_len)
{
self->write_pos = temp;
return true;
}
else
{
return false;
}
}

static void buffer_addreadpos(struct buffer_s* self, size_t value)
static bool buffer_addreadpos(struct buffer_s* self, size_t value)
{
size_t temp = self->read_pos + value;
const size_t temp = self->read_pos + value;
if (temp <= self->data_len)
{
self->read_pos = temp;
return true;
}
else
{
return false;
}
}

Expand All @@ -132,7 +143,7 @@ namespace brynet { namespace base {
}
else
{
return NULL;
return nullptr;
}
}

Expand All @@ -144,11 +155,10 @@ namespace brynet { namespace base {
}
else
{
return NULL;
return nullptr;
}
}


static bool buffer_write(struct buffer_s* self, const char* data, size_t len)
{
bool write_ret = true;
Expand Down
Loading

0 comments on commit 93ddf82

Please sign in to comment.