Skip to content

Commit

Permalink
[tests][memory][fs-memory] Add tests for fs-memory segments
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaZotov committed Apr 15, 2023
1 parent f666de4 commit ba3bae2
Show file tree
Hide file tree
Showing 13 changed files with 429 additions and 73 deletions.
2 changes: 1 addition & 1 deletion sc-memory/sc-core/sc-store/sc-container/sc-list/sc_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ sc_struct_node * sc_list_push(sc_list * list, sc_struct_node * node, void * data

sc_struct_node * sc_list_push_back(sc_list * list, void * data)
{
return sc_list_push(list, list->end ? list->end->prev : null_ptr, data);
return list ? sc_list_push(list, list->end ? list->end->prev : null_ptr, data) : null_ptr;
}

sc_struct_node * sc_list_pop_back(sc_list * list)
Expand Down
4 changes: 4 additions & 0 deletions sc-memory/sc-core/sc-store/sc-container/sc-list/sc_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ sc_struct_node * sc_list_pop_back(sc_list * list);
*/
sc_bool sc_list_remove_if(sc_list * list, void * data, sc_bool (*predicate)(void * data, void * other));

sc_struct_node * sc_list_front(sc_list * list);

sc_struct_node * sc_list_back(sc_list * list);

/*! Gets sc-list forward-backward iterator.
* @param list A sc-list pointer
* @return Returns Iterator.
Expand Down
23 changes: 21 additions & 2 deletions sc-memory/sc-core/sc-store/sc-fs-memory/sc_dictionary_fs_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ sc_dictionary_fs_memory_status sc_dictionary_fs_memory_link_string(
{
if (memory == null_ptr)
{
sc_fs_memory_error("Memory is empty to link string");
sc_fs_memory_info("Memory is empty to link string");
return SC_FS_MEMORY_NO;
}

Expand Down Expand Up @@ -395,7 +395,7 @@ sc_dictionary_fs_memory_status sc_dictionary_fs_memory_unlink_string(
{
if (memory == null_ptr)
{
sc_fs_memory_error("Memory is empty to unlink string");
sc_fs_memory_info("Memory is empty to unlink string");
return SC_FS_MEMORY_NO;
}

Expand Down Expand Up @@ -1382,4 +1382,23 @@ sc_dictionary_fs_memory_status sc_dictionary_fs_memory_save(sc_dictionary_fs_mem
return status;
}

sc_dictionary_fs_memory_status sc_dictionary_fs_memory_clear(sc_dictionary_fs_memory const * memory)
{
sc_fs_memory_info("Clear dictionaries");

if (memory == null_ptr)
{
sc_fs_memory_info("Memory is empty to clear dictionaries");
return SC_FS_MEMORY_NO;
}

if (sc_fs_remove_file(memory->strings_path) == SC_FALSE)
sc_fs_memory_info("Can't remove strings file: %s", memory->strings_path);

if (sc_fs_remove_file(memory->terms_string_offsets_path) == SC_FALSE)
sc_fs_memory_info("Can't remove terms strings offsets file: %s", memory->terms_string_offsets_path);

return SC_FS_MEMORY_OK;
}

#endif
21 changes: 10 additions & 11 deletions sc-memory/sc-core/sc-store/sc-fs-memory/sc_dictionary_fs_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
#include "../sc_defines.h"
#include "../sc_stream.h"

#include "sc_fs_memory_status.h"

#include "../sc-container/sc-dictionary/sc_dictionary.h"
#include "../sc-container/sc-list/sc_list.h"

typedef sc_fs_memory_status sc_dictionary_fs_memory_status;

typedef struct _sc_dictionary_fs_memory
{
sc_char * path; // path to all dictionary files
Expand All @@ -38,17 +42,6 @@ typedef struct _sc_link_hash_content
sc_uint64 string_offset;
} sc_link_hash_content;

typedef enum _sc_dictionary_fs_memory_status
{
SC_FS_MEMORY_OK = 0,

SC_FS_MEMORY_NO, // no memory, it is null_ptr or not correct
SC_FS_MEMORY_NO_STRING,
SC_FS_MEMORY_WRONG_PATH,
SC_FS_MEMORY_WRITE_ERROR,
SC_FS_MEMORY_READ_ERROR
} sc_dictionary_fs_memory_status;

/*! Initialize sc-dictionary file system memory in specified path.
* @param memory[out] A pointer to sc-memory instance
* @param path Path to store on file system
Expand Down Expand Up @@ -211,4 +204,10 @@ sc_dictionary_fs_memory_status sc_dictionary_fs_memory_load(sc_dictionary_fs_mem
*/
sc_dictionary_fs_memory_status sc_dictionary_fs_memory_save(sc_dictionary_fs_memory const * memory);

/*! Clear file system memory
* @param memory A pointer to sc-memory instance
* @returns SC_FS_MEMORY_OK, if are no reading and writing errors.
*/
sc_dictionary_fs_memory_status sc_dictionary_fs_memory_clear(sc_dictionary_fs_memory const * memory);

#endif //_sc_dictionary_fs_memory_h_
30 changes: 9 additions & 21 deletions sc-memory/sc-core/sc-store/sc-fs-memory/sc_fs_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,12 @@ sc_bool sc_fs_memory_initialize(const sc_char * path, sc_uint32 const max_search
if (clear == SC_TRUE)
{
sc_fs_memory_info("Clear sc-memory segments");
if (sc_fs_is_file(manager->segments_path) && g_remove(manager->segments_path) != 0)
{
sc_fs_memory_error("Can't remove segments file: %s", manager->segments_path);
return SC_FALSE;
}
if (sc_fs_remove_file(manager->segments_path) == SC_FALSE)
sc_fs_memory_info("Can't remove segments file: %s", manager->segments_path);

sc_fs_memory_info("Clear sc-fs-memory");
if (manager->clear != null_ptr && manager->clear() != SC_FS_MEMORY_OK)
{
sc_fs_memory_error("Can't clear sc-fs-memory");
return SC_FALSE;
}
if (manager->clear(manager->fs_memory) != SC_FS_MEMORY_OK)
sc_fs_memory_info("Can't clear sc-fs-memory");
}

return SC_TRUE;
Expand Down Expand Up @@ -111,25 +105,22 @@ sc_bool _sc_fs_memory_load_sc_memory_segments(sc_segment ** segments, sc_uint32
sc_fs_memory_info("Load sc-memory segments");
if (sc_fs_is_file(manager->segments_path) == SC_FALSE)
{
*segments_num = 0;
sc_fs_memory_info("There are no sc-memory segments in %s", manager->segments_path);
return SC_TRUE;
}

// open segments
{
sc_io_channel * segments_channel = sc_io_new_read_channel(manager->segments_path, null_ptr);
if (segments_channel == null_ptr)
{
sc_fs_memory_error("Can't open sc-memory segments from: %s", manager->segments_path);
return SC_FALSE;
}
sc_io_channel_set_encoding(segments_channel, null_ptr, null_ptr);

sc_uint64 read_bytes = 0;
if (sc_io_channel_read_chars(segments_channel, (sc_char *)segments_num, sizeof(sc_uint32), &read_bytes, null_ptr) !=
SC_FS_IO_STATUS_NORMAL ||
read_bytes != sizeof(sc_uint32))
{
*segments_num = 0;
sc_fs_memory_error("Error while attribute `segments_num` reading");
sc_io_channel_shutdown(segments_channel, SC_FALSE, null_ptr);
return SC_FALSE;
Expand Down Expand Up @@ -174,11 +165,7 @@ sc_bool _sc_fs_memory_save_sc_memory_segments(sc_segment ** segments, sc_uint32

if (!sc_fs_is_directory(manager->fs_memory->path))
{
if (!sc_fs_create_directory(manager->fs_memory->path))
{
sc_fs_memory_error("Can't create a directory %s", manager->fs_memory->path);
return SC_FALSE;
}
sc_fs_create_directory(manager->fs_memory->path);
}

// create temporary file
Expand All @@ -200,7 +187,8 @@ sc_bool _sc_fs_memory_save_sc_memory_segments(sc_segment ** segments, sc_uint32
{
sc_segment const * segment = segments[idx];

if (sc_io_channel_write_chars(
if (segment == null_ptr ||
sc_io_channel_write_chars(
segments_channel, (sc_char *)segment->elements, SC_SEG_ELEMENTS_SIZE_BYTE, &written_bytes, null_ptr) !=
SC_FS_IO_STATUS_NORMAL ||
written_bytes != SC_SEG_ELEMENTS_SIZE_BYTE)
Expand Down
7 changes: 3 additions & 4 deletions sc-memory/sc-core/sc-store/sc-fs-memory/sc_fs_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
#ifndef _sc_fs_memory_h_
#define _sc_fs_memory_h_

#include "sc_fs_memory_status.h"

#include "../sc_types.h"
#include "../sc_defines.h"
#include "../sc_stream.h"
#include "../sc-container/sc-list/sc_list.h"

#define SC_STORAGE_SEG_CHECKSUM_SIZE 64

#ifdef SC_DICTIONARY_FS_MEMORY
typedef struct _sc_dictionary_fs_memory sc_fs_memory;
typedef enum _sc_dictionary_fs_memory_status sc_fs_memory_status;
#endif

typedef struct _sc_fs_memory_manager
Expand All @@ -26,7 +25,7 @@ typedef struct _sc_fs_memory_manager

sc_fs_memory_status (*initialize)(sc_fs_memory ** memory, const sc_char * path, sc_uint32 max_searchable_string_size);
sc_fs_memory_status (*shutdown)(sc_fs_memory * memory);
sc_fs_memory_status (*clear)();
sc_fs_memory_status (*clear)(sc_fs_memory const * memory);
sc_fs_memory_status (*load)(sc_fs_memory * memory);
sc_fs_memory_status (*save)(sc_fs_memory const * memory);
sc_fs_memory_status (*link_string)(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ sc_fs_memory_manager * sc_fs_memory_build()
manager->shutdown = sc_dictionary_fs_memory_shutdown;
manager->load = sc_dictionary_fs_memory_load;
manager->save = sc_dictionary_fs_memory_save;
manager->clear = null_ptr;
manager->clear = sc_dictionary_fs_memory_clear;
manager->link_string = sc_dictionary_fs_memory_link_string;
manager->get_link_hashes_by_string = sc_dictionary_fs_memory_get_link_hashes_by_string;
manager->get_link_hashes_by_substring = sc_dictionary_fs_memory_get_link_hashes_by_substring_ext;
Expand Down
21 changes: 21 additions & 0 deletions sc-memory/sc-core/sc-store/sc-fs-memory/sc_fs_memory_status.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* This source file is part of an OSTIS project. For the latest info, see http://ostis.net
* Distributed under the MIT License
* (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
*/

#ifndef _sc_fs_memory_status_h_
#define _sc_fs_memory_status_h_

typedef enum _sc_fs_memory_status
{
SC_FS_MEMORY_OK = 0,

SC_FS_MEMORY_NO, // no memory, it is null_ptr or not correct
SC_FS_MEMORY_NO_STRING,
SC_FS_MEMORY_WRONG_PATH,
SC_FS_MEMORY_WRITE_ERROR,
SC_FS_MEMORY_READ_ERROR
} sc_fs_memory_status;

#endif
1 change: 1 addition & 0 deletions sc-memory/tests/sc-memory/containers/sc-dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ TEST(ScDictionaryTest, sc_dictionary_append_get_by_key)
EXPECT_EQ((sc_addr_hash)sc_dictionary_get_by_key(dictionary, string1, string1_size), hash1);

EXPECT_TRUE(_test_sc_uchar_dictionary_destroy(dictionary));
EXPECT_FALSE(_test_sc_uchar_dictionary_destroy(nullptr));
}

TEST(ScDictionaryTest, sc_dictionary_append_get_by_key_reset)
Expand Down
27 changes: 21 additions & 6 deletions sc-memory/tests/sc-memory/containers/sc-list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ extern "C"
#include "sc-core/sc-store/sc-container/sc-iterator/sc_container_iterator.h"
}

#include "sc_test.hpp"

sc_bool scalar_compare(void * value, void * other)
{
return *(sc_uint8 *)value == *(sc_uint8 *)other;
}

using ScListTest = ScMemoryTest;

TEST_F(ScListTest, sc_list)
TEST(ScListTest, sc_list)
{
sc_list * list;
sc_list_init(&list);
Expand Down Expand Up @@ -51,10 +47,26 @@ TEST_F(ScListTest, sc_list)
sc_list_destroy(list);
}

TEST_F(ScListTest, sc_list_iterator)
TEST(ScListTest, sc_list_null_ptr)
{
EXPECT_FALSE(sc_list_init(nullptr));
EXPECT_FALSE(sc_list_destroy(nullptr));
EXPECT_FALSE(sc_list_clear(nullptr));
EXPECT_EQ(sc_list_push(nullptr, nullptr, nullptr), nullptr);
EXPECT_EQ(sc_list_push_back(nullptr, nullptr), nullptr);
EXPECT_EQ(sc_list_pop_back(nullptr), nullptr);
EXPECT_FALSE(sc_list_remove_if(nullptr, nullptr, nullptr));
EXPECT_FALSE(sc_list_remove_if(nullptr, nullptr, nullptr));
EXPECT_EQ(sc_list_front(nullptr), nullptr);
EXPECT_EQ(sc_list_back(nullptr), nullptr);
}

TEST(ScListTest, sc_list_iterator)
{
sc_list * list;
sc_list_init(&list);
EXPECT_EQ(sc_list_front(list), nullptr);
EXPECT_EQ(sc_list_back(list), nullptr);

sc_uint8 size = 10;
auto * values = (sc_uint8 *)malloc(sizeof(sc_uint8) * size);
Expand All @@ -65,6 +77,9 @@ TEST_F(ScListTest, sc_list_iterator)
sc_list_push_back(list, (void *)(&values[i]));
}

EXPECT_NE(sc_list_front(list), nullptr);
EXPECT_NE(sc_list_back(list), nullptr);

i = 0;
void * data;
sc_iterator * it = sc_list_iterator(list);
Expand Down
Loading

0 comments on commit ba3bae2

Please sign in to comment.