mirror of
https://github.com/usatiuk/ficus.git
synced 2025-10-29 00:27:52 +01:00
KApi for tests
This commit is contained in:
@@ -5,13 +5,13 @@
|
||||
#include <utility>
|
||||
|
||||
#include "assert.h"
|
||||
#include "stdlib.h"
|
||||
#include "kmem.hpp"
|
||||
#include "string.h"
|
||||
|
||||
class String {
|
||||
public:
|
||||
String() noexcept {
|
||||
_data = static_cast<char *>(malloc(1 * sizeof(char)));
|
||||
_data = static_cast<char *>(kmalloc(1 * sizeof(char)));
|
||||
curLen = 0;
|
||||
_data[0] = '\0';
|
||||
}
|
||||
@@ -19,7 +19,7 @@ public:
|
||||
String(const char *in) noexcept {
|
||||
curLen = strlen(in);
|
||||
|
||||
_data = static_cast<char *>(malloc((curLen + 1) * sizeof(char)));
|
||||
_data = static_cast<char *>(kmalloc((curLen + 1) * sizeof(char)));
|
||||
_data[0] = '\0';
|
||||
|
||||
strcat(_data, in);
|
||||
@@ -28,7 +28,7 @@ public:
|
||||
String(String const &str) noexcept {
|
||||
curLen = str.curLen;
|
||||
|
||||
_data = static_cast<char *>(malloc((curLen + 1) * sizeof(char)));
|
||||
_data = static_cast<char *>(kmalloc((curLen + 1) * sizeof(char)));
|
||||
_data[0] = '\0';
|
||||
|
||||
strcat(_data, str._data);
|
||||
@@ -38,7 +38,7 @@ public:
|
||||
_data = str._data;
|
||||
curLen = str.curLen;
|
||||
|
||||
str._data = static_cast<char *>(malloc(1 * sizeof(char)));
|
||||
str._data = static_cast<char *>(kmalloc(1 * sizeof(char)));
|
||||
str.curLen = 0;
|
||||
str._data[0] = '\0';
|
||||
}
|
||||
@@ -51,13 +51,13 @@ public:
|
||||
|
||||
~String() noexcept {
|
||||
if (_data == nullptr) return;
|
||||
free(_data);
|
||||
kfree(_data);
|
||||
_data = nullptr;
|
||||
curLen = 0;
|
||||
}
|
||||
|
||||
String &operator+=(String const &rhs) {
|
||||
_data = static_cast<char *>(realloc(_data, sizeof(char) * (curLen + rhs.curLen + 1)));
|
||||
_data = static_cast<char *>(krealloc(_data, sizeof(char) * (curLen + rhs.curLen + 1)));
|
||||
assert(_data != nullptr);
|
||||
|
||||
strcat(_data, rhs._data);
|
||||
@@ -84,7 +84,7 @@ public:
|
||||
}
|
||||
|
||||
String &operator+=(char c) {
|
||||
_data = static_cast<char *>(realloc(_data, sizeof(char) * (curLen + 2)));
|
||||
_data = static_cast<char *>(krealloc(_data, sizeof(char) * (curLen + 2)));
|
||||
assert(_data != nullptr);
|
||||
|
||||
_data[curLen] = c;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <new>
|
||||
|
||||
#include "assert.h"
|
||||
#include "kmem.hpp"
|
||||
#include "string.h"
|
||||
|
||||
class VectorTester;
|
||||
@@ -14,14 +15,14 @@ class Vector {
|
||||
|
||||
public:
|
||||
Vector() noexcept {
|
||||
data = static_cast<T *>(malloc(capacity * sizeof(T)));
|
||||
data = static_cast<T *>(kmalloc(capacity * sizeof(T)));
|
||||
}
|
||||
|
||||
Vector(std::initializer_list<T> l) noexcept {
|
||||
curSize = l.size();
|
||||
capacity = curSize > 0 ? curSize : 2;
|
||||
|
||||
data = static_cast<T *>(malloc(capacity * sizeof(T)));
|
||||
data = static_cast<T *>(kmalloc(capacity * sizeof(T)));
|
||||
|
||||
size_t i = 0;
|
||||
for (auto const &el: l) {
|
||||
@@ -33,7 +34,7 @@ public:
|
||||
curSize = vec.curSize;
|
||||
capacity = curSize > 0 ? curSize : 2;
|
||||
|
||||
data = static_cast<T *>(malloc(capacity * sizeof(T)));
|
||||
data = static_cast<T *>(kmalloc(capacity * sizeof(T)));
|
||||
|
||||
for (size_t i = 0; i < curSize; i++)
|
||||
new (data + i) T(vec.data[i]);
|
||||
@@ -66,14 +67,14 @@ public:
|
||||
if (capacity == curSize) {
|
||||
capacity *= 2;
|
||||
//Ugly hack to get around g++ warnings
|
||||
data = (T *) realloc(reinterpret_cast<char *>(data), capacity * sizeof(T));
|
||||
data = (T *) krealloc(reinterpret_cast<char *>(data), capacity * sizeof(T));
|
||||
assert(data != nullptr);
|
||||
}
|
||||
new (data + (curSize++)) T(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
void compact() {
|
||||
data = (T *) realloc(reinterpret_cast<char *>(data), curSize * sizeof(T));
|
||||
data = (T *) krealloc(reinterpret_cast<char *>(data), curSize * sizeof(T));
|
||||
capacity = curSize;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,4 +10,5 @@ FetchContent_Declare(
|
||||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
||||
FetchContent_MakeAvailable(googletest)
|
||||
|
||||
add_subdirectory(kapi)
|
||||
add_subdirectory(templates)
|
||||
5
src/unit-tests/kapi/CMakeLists.txt
Normal file
5
src/unit-tests/kapi/CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
add_library(KApi
|
||||
INTERFACE
|
||||
)
|
||||
|
||||
target_include_directories(KApi INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
17
src/unit-tests/kapi/kmem.hpp
Normal file
17
src/unit-tests/kapi/kmem.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 10.04.2024.
|
||||
//
|
||||
|
||||
#ifndef KMEM_HPP
|
||||
#define KMEM_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
|
||||
void *kmalloc(size_t n) {return malloc(n);}
|
||||
void kfree(void *addr){return free(addr);}
|
||||
void *krealloc(void *addr, size_t newsize) {return realloc(addr,newsize);}
|
||||
|
||||
|
||||
#endif //KMEM_HPP
|
||||
42
src/unit-tests/kapi/stdlib.h
Normal file
42
src/unit-tests/kapi/stdlib.h
Normal file
@@ -0,0 +1,42 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 10.04.2024.
|
||||
//
|
||||
|
||||
#ifndef STDLIB_H
|
||||
#define STDLIB_H
|
||||
|
||||
#include_next <stdlib.h>
|
||||
|
||||
static inline char *itoa(int value, char *str, int base) {
|
||||
char *rc;
|
||||
char *ptr;
|
||||
char *low;
|
||||
// Check for supported base.
|
||||
if (base < 2 || base > 36) {
|
||||
*str = '\0';
|
||||
return str;
|
||||
}
|
||||
rc = ptr = str;
|
||||
// Set '-' for negative decimals.
|
||||
if (value < 0 && base == 10) {
|
||||
*ptr++ = '-';
|
||||
}
|
||||
// Remember where the numbers start.
|
||||
low = ptr;
|
||||
// The actual conversion.
|
||||
do {
|
||||
// Modulo is negative for negative value. This trick makes abs() unnecessary.
|
||||
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + value % base];
|
||||
value /= base;
|
||||
} while (value);
|
||||
// Terminating the string.
|
||||
*ptr-- = '\0';
|
||||
// Invert the numbers.
|
||||
while (low < ptr) {
|
||||
char tmp = *low;
|
||||
*low++ = *ptr;
|
||||
*ptr-- = tmp;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
#endif //STDLIB_H
|
||||
@@ -2,6 +2,9 @@ add_executable(
|
||||
SkipListTest
|
||||
SkipListTest.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(templates INTERFACE KApi)
|
||||
|
||||
target_link_libraries(
|
||||
SkipListTest
|
||||
templates
|
||||
|
||||
@@ -1,41 +1,6 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <SkipList.hpp>
|
||||
|
||||
// FIXME
|
||||
char *itoa(int value, char *str, int base) {
|
||||
char *rc;
|
||||
char *ptr;
|
||||
char *low;
|
||||
// Check for supported base.
|
||||
if (base < 2 || base > 36) {
|
||||
*str = '\0';
|
||||
return str;
|
||||
}
|
||||
rc = ptr = str;
|
||||
// Set '-' for negative decimals.
|
||||
if (value < 0 && base == 10) {
|
||||
*ptr++ = '-';
|
||||
}
|
||||
// Remember where the numbers start.
|
||||
low = ptr;
|
||||
// The actual conversion.
|
||||
do {
|
||||
// Modulo is negative for negative value. This trick makes abs() unnecessary.
|
||||
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + value % base];
|
||||
value /= base;
|
||||
} while (value);
|
||||
// Terminating the string.
|
||||
*ptr-- = '\0';
|
||||
// Invert the numbers.
|
||||
while (low < ptr) {
|
||||
char tmp = *low;
|
||||
*low++ = *ptr;
|
||||
*ptr-- = tmp;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
#include <String.hpp>
|
||||
#include <string>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user