<commit_before>
#include <togo/core/error/assert.hpp>
#include <togo/core/log/log.hpp>
#include <togo/core/collection/fixed_array.hpp>
#include <togo/core/string/string.hpp>
#include <togo/core/system/system.hpp>
#include <togo/core/filesystem/filesystem.hpp>

using namespace togo;

signed main() {
	StringRef const exec_dir = filesystem::exec_dir();
	TOGO_LOGF("exec_dir: '%.*s'\n", exec_dir.size, exec_dir.data);

	// Working directory
	FixedArray<char, 256> wd_init{};
	unsigned const wd_init_size = filesystem::working_dir(wd_init);
	TOGO_LOGF("wd_init: '%.*s'\n", string::size(wd_init), fixed_array::begin(wd_init));
	TOGO_ASSERTE(wd_init_size > 0 && wd_init_size == fixed_array::size(wd_init));

	// Change directory
	TOGO_ASSERTE(filesystem::set_working_dir("/"));
	FixedArray<char, 256> wd_root{};
	unsigned const wd_root_size = filesystem::working_dir(wd_root);
	TOGO_LOGF("wd_root: '%.*s'\n", string::size(wd_root), fixed_array::begin(wd_root));
	TOGO_ASSERTE(wd_root_size > 0 && wd_root_size == fixed_array::size(wd_root));
	TOGO_ASSERTE(string::compare_equal(wd_root, "/"));

	// Move to group dir
	TOGO_ASSERTE(filesystem::set_working_dir(exec_dir));

	// Invariants based on test & filesystem
	TOGO_ASSERTE(!filesystem::is_file("/non/existent/file"));
	TOGO_ASSERTE(!filesystem::is_file("."));
	#if defined(TOGO_PLATFORM_LINUX)
		TOGO_ASSERTE(filesystem::is_file("general.elf"));
	#elif defined(TOGO_PLATFORM_WINDOWS)
		TOGO_ASSERTE(filesystem::is_file("general.exe"));
	#endif

	TOGO_ASSERTE(!filesystem::is_directory("/non/existent/directory"));
	TOGO_ASSERTE( filesystem::is_directory("."));
	TOGO_ASSERTE( filesystem::is_directory(".."));

	#define TEST_DIR "test_dir"
	#define TEST_FILE TEST_DIR "/test_file"
	#define TEST_FILE_MOVED TEST_DIR "/test_file_moved"

	// Directory creation
	TOGO_ASSERTE(!filesystem::is_directory(TEST_DIR));
	TOGO_ASSERTE(filesystem::create_directory(TEST_DIR));
	TOGO_ASSERTE(filesystem::is_directory(TEST_DIR));
	TOGO_ASSERTE(!filesystem::create_directory(TEST_DIR));
	TOGO_ASSERTE(
		filesystem::time_last_modified(TEST_DIR) >
		system::secs_since_epoch() - 2
	);

	// File creation
	TOGO_ASSERTE(!filesystem::is_file(TEST_FILE));
	TOGO_ASSERTE(filesystem::create_file(TEST_FILE));
	TOGO_ASSERTE(filesystem::is_file(TEST_FILE));
	TOGO_ASSERTE(!filesystem::create_file(TEST_FILE));
	TOGO_ASSERTE(
		filesystem::time_last_modified(TEST_FILE) >
		system::secs_since_epoch() - 2
	);
	TOGO_ASSERTE(filesystem::file_size(TEST_FILE) == 0);

	// File move
	TOGO_ASSERTE(filesystem::move_file(TEST_FILE, TEST_FILE_MOVED));
	TOGO_ASSERTE(!filesystem::is_file(TEST_FILE));
	TOGO_ASSERTE(filesystem::is_file(TEST_FILE_MOVED));
	TOGO_ASSERTE(!filesystem::move_file(TEST_FILE, TEST_FILE_MOVED));

	TOGO_ASSERTE(filesystem::move_file(TEST_FILE_MOVED, TEST_FILE));
	TOGO_ASSERTE(filesystem::is_file(TEST_FILE));
	TOGO_ASSERTE(!filesystem::is_file(TEST_FILE_MOVED));

	// File removal
	TOGO_ASSERTE(filesystem::remove_file(TEST_FILE));
	TOGO_ASSERTE(!filesystem::is_file(TEST_FILE));
	TOGO_ASSERTE(!filesystem::remove_file(TEST_FILE));
	TOGO_ASSERTE(filesystem::file_size(TEST_FILE) == 0);

	// Directory removal
	TOGO_ASSERTE(filesystem::remove_directory(TEST_DIR));
	TOGO_ASSERTE(!filesystem::is_directory(TEST_DIR));
	TOGO_ASSERTE(!filesystem::remove_directory(TEST_DIR));

	return 0;
}
<commit_msg>lib/core/test/filesystem/general: added copy_file() test; test create_file() with overwrite = true.<commit_after>
#include <togo/core/error/assert.hpp>
#include <togo/core/log/log.hpp>
#include <togo/core/collection/fixed_array.hpp>
#include <togo/core/string/string.hpp>
#include <togo/core/system/system.hpp>
#include <togo/core/filesystem/filesystem.hpp>

using namespace togo;

signed main() {
	StringRef const exec_dir = filesystem::exec_dir();
	TOGO_LOGF("exec_dir: '%.*s'\n", exec_dir.size, exec_dir.data);

	// Working directory
	FixedArray<char, 256> wd_init{};
	unsigned const wd_init_size = filesystem::working_dir(wd_init);
	TOGO_LOGF("wd_init: '%.*s'\n", string::size(wd_init), fixed_array::begin(wd_init));
	TOGO_ASSERTE(wd_init_size > 0 && wd_init_size == fixed_array::size(wd_init));

	// Change directory
	TOGO_ASSERTE(filesystem::set_working_dir("/"));
	FixedArray<char, 256> wd_root{};
	unsigned const wd_root_size = filesystem::working_dir(wd_root);
	TOGO_LOGF("wd_root: '%.*s'\n", string::size(wd_root), fixed_array::begin(wd_root));
	TOGO_ASSERTE(wd_root_size > 0 && wd_root_size == fixed_array::size(wd_root));
	TOGO_ASSERTE(string::compare_equal(wd_root, "/"));

	// Move to group dir
	TOGO_ASSERTE(filesystem::set_working_dir(exec_dir));

	// Invariants based on test & filesystem
	TOGO_ASSERTE(!filesystem::is_file("/non/existent/file"));
	TOGO_ASSERTE(!filesystem::is_file("."));
	#if defined(TOGO_PLATFORM_LINUX)
		#define TEST_EXEC_FILE "general.elf"
	#elif defined(TOGO_PLATFORM_WINDOWS)
		#define TEST_EXEC_FILE "general.exe"
	#endif
	TOGO_ASSERTE(filesystem::is_file(TEST_EXEC_FILE));

	TOGO_ASSERTE(!filesystem::is_directory("/non/existent/directory"));
	TOGO_ASSERTE( filesystem::is_directory("."));
	TOGO_ASSERTE( filesystem::is_directory(".."));

	#define TEST_DIR "test_dir"
	#define TEST_FILE TEST_DIR "/test_file"
	#define TEST_FILE_MOVED TEST_DIR "/test_file_moved"
	#define TEST_FILE_COPIED TEST_DIR "/test_file_copied"

	// Directory creation
	TOGO_ASSERTE(!filesystem::is_directory(TEST_DIR));
	TOGO_ASSERTE(filesystem::create_directory(TEST_DIR));
	TOGO_ASSERTE(filesystem::is_directory(TEST_DIR));
	TOGO_ASSERTE(!filesystem::create_directory(TEST_DIR));
	TOGO_ASSERTE(
		filesystem::time_last_modified(TEST_DIR) >
		system::secs_since_epoch() - 2
	);

	// File creation
	TOGO_ASSERTE(!filesystem::is_file(TEST_FILE));
	TOGO_ASSERTE(filesystem::create_file(TEST_FILE));
	TOGO_ASSERTE(filesystem::is_file(TEST_FILE));
	TOGO_ASSERTE(!filesystem::create_file(TEST_FILE));
	TOGO_ASSERTE(filesystem::create_file(TEST_FILE, true));
	TOGO_ASSERTE(
		filesystem::time_last_modified(TEST_FILE) >
		system::secs_since_epoch() - 2
	);
	TOGO_ASSERTE(filesystem::file_size(TEST_FILE) == 0);

	// File copy
	TOGO_ASSERTE(filesystem::copy_file(TEST_EXEC_FILE, TEST_FILE_COPIED));
	TOGO_ASSERTE(filesystem::is_file(TEST_FILE_COPIED));
	TOGO_ASSERTE(filesystem::file_size(TEST_EXEC_FILE) == filesystem::file_size(TEST_FILE_COPIED));

	TOGO_ASSERTE(!filesystem::copy_file(TEST_FILE, TEST_FILE_COPIED));
	TOGO_ASSERTE(filesystem::copy_file(TEST_FILE, TEST_FILE_COPIED, true));
	TOGO_ASSERTE(filesystem::file_size(TEST_FILE) == filesystem::file_size(TEST_FILE_COPIED));

	// File move
	TOGO_ASSERTE(filesystem::move_file(TEST_FILE, TEST_FILE_MOVED));
	TOGO_ASSERTE(!filesystem::is_file(TEST_FILE));
	TOGO_ASSERTE(filesystem::is_file(TEST_FILE_MOVED));
	TOGO_ASSERTE(!filesystem::move_file(TEST_FILE, TEST_FILE_MOVED));

	TOGO_ASSERTE(filesystem::move_file(TEST_FILE_MOVED, TEST_FILE));
	TOGO_ASSERTE(filesystem::is_file(TEST_FILE));
	TOGO_ASSERTE(!filesystem::is_file(TEST_FILE_MOVED));

	// File removal
	TOGO_ASSERTE(filesystem::remove_file(TEST_FILE));
	TOGO_ASSERTE(!filesystem::is_file(TEST_FILE));
	TOGO_ASSERTE(!filesystem::remove_file(TEST_FILE));
	TOGO_ASSERTE(filesystem::file_size(TEST_FILE) == 0);

	TOGO_ASSERTE(filesystem::remove_file(TEST_FILE_COPIED));
	TOGO_ASSERTE(!filesystem::is_file(TEST_FILE_COPIED));
	TOGO_ASSERTE(!filesystem::remove_file(TEST_FILE_COPIED));

	// Directory removal
	TOGO_ASSERTE(filesystem::remove_directory(TEST_DIR));
	TOGO_ASSERTE(!filesystem::is_directory(TEST_DIR));
	TOGO_ASSERTE(!filesystem::remove_directory(TEST_DIR));

	return 0;
}

{'fraction_non_alphanumeric': 0.14963552021206097, 'fraction_numerical': 0.0034459907223326706, 'mean_word_length': 9.379642365887207, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1670760', 'n_tokens_mistral': 3014, 'n_tokens_neox': 2911, 'n_words': 326}