Newer
Older
c-interpreter / modules / main / ut / src / ut_kc_memory_dump.cpp
Nomura Kei on 9 Aug 2023 2 KB UPDATE
#include <kcpp_unittest.hpp>
#include <cstring>

#include <kc_memory_dump.h>

using namespace kcpp;
using namespace kcpp::Assert;
using namespace kc;

/**
 * KcMemoryDump 単体テスト。
 */
class UtKcMemoryDump: public TestCase
{
	public:
		UtKcMemoryDump() : entry()
				{ /* NOP */ }
		~UtKcMemoryDump() {/* NOP */ }

		void setUp()
		{
			static char data_buff[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
			entry.file = "file";
			entry.func = "func";
			entry.line = 123;
			entry.size = 52;
			entry.data = data_buff;

			snprintf(expected_info  , sizeof(expected_info)  , "file:123 (10 bytes) [func=func]");
			snprintf(expected_binary, sizeof(expected_binary), " | 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F");

		}

		void testSample1()
		{
			int  buff_size  = 81;//sizeof(test_buff);
			int  bytes      = 60;
			bool binary     = true;
			bool ascii      = true;
			int  column     = 80;

			kc_memory_dump(test_buff, buff_size, &entry, bytes, binary, ascii, column);

			// カラム数が合致するか確認
			printf("         10        20        30        40        50        60        70        80        90\n");
			printf("----+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|\n");
			printf("%s[EOL]\n", test_buff);
		}
		void testSample2()
		{
		}
		void run()
		{
			RUN_TEST(testSample1, "sample1");
		}
	private:
		KcMemoryEntry entry;
		char test_buff[4096];
		char expected_info[4096];
		char expected_binary[4096];
		char expected_ascii[4096];
};

#if 0
#ifndef UNITTEST
int main()
#else
int dummy()
#endif
{
	char buff[1024];
	KcMemoryEntry entry = {
		.file = "file",
		.func = "func",
		.line = 123,
		.size = 10,
		.data = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	};

	TestInfo testInfoList[] =
	{
		{ .buff_size = 100, .bytes = 16, .binary = true, .ascii = false, .max_column = 80 },
		{ .buff_size = 100, .bytes = 16, .binary = true, .ascii = true, .max_column = 80 },
		{ .buff_size = 100, .bytes = 16, .binary = true, .ascii = true, .max_column = 79 },


		/* 番兵 */
		{ .buff_size = -1, .bytes = -1, .binary = false, .ascii = false, .max_column = -1 }
	};

	for (TestInfo* testInfo = &testInfoList[0]; testInfo->buff_size >= 0; testInfo++)
	{

		kc_memory_dump(buff, testInfo->buff_size, &entry, testInfo->bytes, testInfo->binary, testInfo->ascii, testInfo->max_column);

		printf("####################################################\n");
		printf("buff_size = %d\n", testInfo->buff_size);
		printf("bytes     = %d\n", testInfo->bytes);
		printf("max_column= %d\n", testInfo->max_column);
		printf("         10        20        30        40        50        60        70        80        90         \n");
		printf("----+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|\n");
		printf("%s[END]\n", buff);

	}
	return 0;
}
#endif