<commit_before>#include <cybozu/file.hpp>
#include <cybozu/test.hpp>
#include <memory.h>
#include <stdlib.h>
#include <vector>

static const std::string fileName = "test.tmp";
static const std::string fileName2 = "test2.tmp";
static const char testBuf[] = { 1, 2, 5, 4, 3, 2, 1 };

CYBOZU_TEST_AUTO(open_and_write)
{
	cybozu::File f;
	CYBOZU_TEST_ASSERT(!f.isOpen());
	CYBOZU_TEST_ASSERT(f.open(fileName, std::ios::out | std::ios::trunc, cybozu::DontThrow));
	CYBOZU_TEST_ASSERT(f.isOpen());
	CYBOZU_TEST_ASSERT(f.write(testBuf, sizeof(testBuf), cybozu::DontThrow));
}

CYBOZU_TEST_AUTO(read)
{
	cybozu::File f;
	CYBOZU_TEST_ASSERT(f.open(fileName, std::ios::in, cybozu::DontThrow));
	char buf[sizeof(testBuf)];
	CYBOZU_TEST_EQUAL(f.read(buf, sizeof(buf), cybozu::DontThrow), (int)sizeof(testBuf));
	CYBOZU_TEST_ASSERT(memcmp(buf, testBuf, sizeof(buf)) == 0);
	CYBOZU_TEST_EQUAL(f.getSize(), (int)sizeof(buf));
}

CYBOZU_TEST_AUTO(size)
{
	CYBOZU_TEST_EQUAL(cybozu::GetFileSize(fileName), sizeof(testBuf));
}

CYBOZU_TEST_AUTO(append)
{
	cybozu::File f;
	CYBOZU_TEST_ASSERT(!f.isOpen());
	CYBOZU_TEST_ASSERT(f.open(fileName, std::ios::out | std::ios::app, cybozu::DontThrow));
	CYBOZU_TEST_ASSERT(f.isOpen());
	CYBOZU_TEST_ASSERT(f.write(testBuf, sizeof(testBuf), cybozu::DontThrow));
}

CYBOZU_TEST_AUTO(read2)
{
	cybozu::File f;
	CYBOZU_TEST_ASSERT(f.open(fileName, std::ios::in, cybozu::DontThrow));
	const int size = sizeof(testBuf);
	char buf[size * 2];
	CYBOZU_TEST_EQUAL(f.read(buf, size * 2, cybozu::DontThrow), size * 2);
	CYBOZU_TEST_ASSERT(memcmp(buf, testBuf, size) == 0);
	CYBOZU_TEST_ASSERT(memcmp(buf + size, testBuf, size) == 0);
	CYBOZU_TEST_EQUAL(f.getSize(), size * 2);
}

CYBOZU_TEST_AUTO(badmode)
{
	cybozu::File f;
	CYBOZU_TEST_ASSERT(!f.open("test", std::ios::in | std::ios::out));
	CYBOZU_TEST_ASSERT(!f.open("test", std::ios::in | std::ios::trunc));
	CYBOZU_TEST_ASSERT(!f.open("test", std::ios::in | std::ios::app));
	CYBOZU_TEST_ASSERT(!f.open("test", std::ios::out | std::ios::trunc | std::ios::app));
}

CYBOZU_TEST_AUTO(move_and_remove)
{
	{
		cybozu::File f;
		CYBOZU_TEST_ASSERT(f.open(fileName, std::ios::out | std::ios::trunc));
		CYBOZU_TEST_ASSERT(f.isOpen());
		CYBOZU_TEST_ASSERT(f.write(testBuf, sizeof(testBuf), cybozu::DontThrow));
	}
	{
		/*
			remove fileName2 if exists and move fileName to fileName2
		*/
		if (cybozu::DoesFileExist(fileName2)) {
			cybozu::RemoveFile(fileName2);
		}
		CYBOZU_TEST_ASSERT(!cybozu::DoesFileExist(fileName2));
		cybozu::RenameFile(fileName, fileName2);
		CYBOZU_TEST_ASSERT(cybozu::DoesFileExist(fileName2));
		cybozu::RemoveFile(fileName2);
	}
	{
		CYBOZU_TEST_EXCEPTION_MESSAGE(cybozu::RemoveFile(fileName2), cybozu::Exception, fileName2);
		CYBOZU_TEST_EXCEPTION_MESSAGE(cybozu::RenameFile(fileName, fileName2), cybozu::Exception, fileName2);
	}
}

CYBOZU_TEST_AUTO(path)
{
	{
		std::string a = "abc\\def\\defg\\\\";
		std::string b = "abc/def/defg//";
		cybozu::ReplaceBackSlash(a);
		CYBOZU_TEST_EQUAL(a, b);
	}<commit_msg>file test for new api<commit_after>#include <cybozu/file.hpp>
#include <cybozu/test.hpp>
#include <memory.h>
#include <stdlib.h>
#include <vector>

static const std::string fileName = "test.tmp";
static const std::string fileName2 = "test2.tmp";
static const char testBuf[] = { 1, 2, 5, 4, 3, 2, 1 };

CYBOZU_TEST_AUTO(open_and_write)
{
	cybozu::File f;
	CYBOZU_TEST_ASSERT(!f.isOpen());
	f.open(fileName, std::ios::out | std::ios::trunc);
	CYBOZU_TEST_ASSERT(f.isOpen());
	f.write(testBuf, sizeof(testBuf));
	f.close();
}

CYBOZU_TEST_AUTO(read)
{
	cybozu::File f;
	f.open(fileName, std::ios::in);
	CYBOZU_TEST_ASSERT(f.isOpen());
	char buf[sizeof(testBuf)] = {};
	f.read(buf, sizeof(buf));
	CYBOZU_TEST_ASSERT(memcmp(buf, testBuf, sizeof(buf)) == 0);
	CYBOZU_TEST_EQUAL(f.getSize(), sizeof(testBuf));
}

CYBOZU_TEST_AUTO(size)
{
	CYBOZU_TEST_EQUAL(cybozu::GetFileSize(fileName), sizeof(testBuf));
}

CYBOZU_TEST_AUTO(append)
{
	cybozu::File f;
	CYBOZU_TEST_ASSERT(!f.isOpen());
	f.open(fileName, std::ios::out | std::ios::app);
	CYBOZU_TEST_ASSERT(f.isOpen());
	f.write(testBuf, sizeof(testBuf));
}

CYBOZU_TEST_AUTO(read2)
{
	cybozu::File f;
	f.open(fileName, std::ios::in);
	const size_t size = sizeof(testBuf);
	char buf[size * 2];
	f.read(buf, size * 2);
	CYBOZU_TEST_ASSERT(memcmp(buf, testBuf, size) == 0);
	CYBOZU_TEST_ASSERT(memcmp(buf + size, testBuf, size) == 0);
	CYBOZU_TEST_EQUAL(f.getSize(), size * 2);
}

CYBOZU_TEST_AUTO(badmode)
{
	cybozu::File f;
	CYBOZU_TEST_EXCEPTION(f.open("test", std::ios::in | std::ios::out), cybozu::Exception);
	CYBOZU_TEST_EXCEPTION(f.open("test", std::ios::in | std::ios::trunc), cybozu::Exception);
	CYBOZU_TEST_EXCEPTION(f.open("test", std::ios::in | std::ios::app), cybozu::Exception);
	CYBOZU_TEST_EXCEPTION(f.open("test", std::ios::out | std::ios::trunc | std::ios::app), cybozu::Exception);
}

CYBOZU_TEST_AUTO(move_and_remove)
{
	{
		cybozu::File f;
		f.open(fileName, std::ios::out | std::ios::trunc);
		CYBOZU_TEST_ASSERT(f.isOpen());
		f.write(testBuf, sizeof(testBuf));
	}
	{
		/*
			remove fileName2 if exists and move fileName to fileName2
		*/
		if (cybozu::DoesFileExist(fileName2)) {
			cybozu::RemoveFile(fileName2);
		}
		CYBOZU_TEST_ASSERT(!cybozu::DoesFileExist(fileName2));
		cybozu::RenameFile(fileName, fileName2);
		CYBOZU_TEST_ASSERT(cybozu::DoesFileExist(fileName2));
		cybozu::RemoveFile(fileName2);
	}
	{
		CYBOZU_TEST_EXCEPTION_MESSAGE(cybozu::RemoveFile(fileName2), cybozu::Exception, fileName2);
		CYBOZU_TEST_EXCEPTION_MESSAGE(cybozu::RenameFile(fileName, fileName2), cybozu::Exception, fileName2);
	}
}

CYBOZU_TEST_AUTO(path)
{
	{
		std::string a = "abc\\def\\defg\\\\";
		std::string b = "abc/def/defg//";
		cybozu::ReplaceBackSlash(a);
		CYBOZU_TEST_EQUAL(a, b);
	}
{'fraction_non_alphanumeric': 0.18129671475751782, 'fraction_numerical': 0.009907874152616026, 'mean_word_length': 7.196581196581197, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '5489682', 'n_tokens_mistral': 2545, 'n_tokens_neox': 2513, 'n_words': 332}