diff --git a/include/blt/parse/argparse.h b/include/blt/parse/argparse.h index 5625d27..0f85c42 100755 --- a/include/blt/parse/argparse.h +++ b/include/blt/parse/argparse.h @@ -287,12 +287,15 @@ namespace blt inline bool contains(const std::string& key) { + if (key.starts_with("--")) + return data.find(key.substr(2)) != data.end(); + if (key.starts_with('-')) + return data.find(key.substr(1)) != data.end(); return data.find(key) != data.end(); } } loaded_args; private: - static std::string filename(const std::string& path); static std::string getMetavar(const arg_properties_t* const& arg); static std::string getFlagHelp(const arg_properties_t* const& arg); @@ -366,6 +369,8 @@ namespace blt user_args.max_line_length = size; } + static std::string filename(const std::string& path); + ~arg_parse() { for (auto* p : user_args.arg_properties_storage) diff --git a/src/blt/parse/argparse.cpp b/src/blt/parse/argparse.cpp index 39d8d5d..09ba022 100755 --- a/src/blt/parse/argparse.cpp +++ b/src/blt/parse/argparse.cpp @@ -495,7 +495,7 @@ namespace blt void arg_parse::printUsage() const { - std::string usage = "Usage: " + loaded_args.program_name + " "; + std::string usage = "Usage: " + filename(loaded_args.program_name) + " "; std::cout << usage; size_t current_line_length = 0; diff --git a/src/tests/main.cpp b/src/tests/main.cpp index 371b801..ffcc664 100755 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -13,26 +13,31 @@ //#include std::function test{ - [](int i) -> int { - int acc = 1; - for (int j = 0; j < i; j++){ - acc += j * i; + [](int i) -> int { + int acc = 1; + for (int j = 0; j < i; j++) + { + acc += j * i; + } + return acc; } - return acc; - } }; -int test_as_func(int i){ +int test_as_func(int i) +{ int acc = 1; - for (int j = 0; j < i; j++){ + for (int j = 0; j < i; j++) + { acc += j * i; } return acc; } -inline int test_as_func_inline(int i){ +inline int test_as_func_inline(int i) +{ int acc = 1; - for (int j = 0; j < i; j++){ + for (int j = 0; j < i; j++) + { acc += j * i; } return acc; @@ -40,61 +45,69 @@ inline int test_as_func_inline(int i){ std::function test_func_as_std(&test_as_func); -class super_func { +class super_func +{ public: virtual int test(int i) = 0; + virtual ~super_func() = default; }; -class class_func : public super_func { +class class_func : public super_func +{ public: - int test(int i) override { + int test(int i) override + { int acc = 1; - for (int j = 0; j < i; j++){ + for (int j = 0; j < i; j++) + { acc += j * i; } return acc; } }; -int (*func_func)(int) = [](int i) -> int { +int (* func_func)(int) = [](int i) -> int { int acc = 1; - for (int j = 0; j < i; j++){ + for (int j = 0; j < i; j++) + { acc += j * i; } return acc; }; -int (*func_func_in)(int) = &test_as_func; +int (* func_func_in)(int) = &test_as_func; -int main(int argc, const char** argv) { +int main(int argc, const char** argv) +{ blt::arg_parse parser; - parser.addArgument(blt::arg_builder({"--poo", "-p"}).build()); - parser.addArgument(blt::arg_builder("--foo").setAction(blt::arg_action_t::STORE_TRUE).setDefault(false).build()); - parser.addArgument(blt::arg_builder({"--goo", "-g"}).build()); - parser.addArgument(blt::arg_builder({"--oop", "-o"}).build()); - parser.addArgument(blt::arg_builder("Sexy_pos").setHelp("I am helpful!").build()); + parser.addArgument(blt::arg_builder({"-c", "--no-color"}).setAction(blt::arg_action_t::STORE_TRUE).build()); + parser.addArgument( + blt::arg_builder("--nbt") + .setHelp("Run NBT tests. Accepts optional # of bytes to write. Default: 1mb") + .setMetavar("bytes") + .setAction(blt::arg_action_t::STORE) + .setNArgs('?').build()); auto args = parser.parse_args(argc, argv); - std::vector superArgs { - "BLT_TESTS", - "Sexy", - "-p", "I have poop", - "--help" - }; - auto args2 = parser.parse_args(superArgs); - for (const auto& a : args2.data){ - BLT_TRACE("['%s' = '%s']", a.first.c_str(), blt::to_string(a.second).c_str()); + if (args.contains("--no-color")) + { + for (int i = (int) blt::logging::log_level::NONE; i < (int) blt::logging::log_level::FATAL; i++) + blt::logging::setLogColor((blt::logging::log_level) i, ""); + blt::logging::setLogOutputFormat("[${{TIME}}] [${{LOG_LEVEL}}] (${{FILE}}:${{LINE}}) ${{STR}}\n"); } - BLT_TRACE(args2.program_name); -// -// if (argc > 1 && std::string(argv[1]) == "--no_color") { -// for (int i = (int)blt::logging::log_level::NONE; i < (int)blt::logging::log_level::FATAL; i++) { -// blt::logging::setLogColor((blt::logging::log_level)i, ""); -// } -// blt::logging::setLogOutputFormat("[${{TIME}}] [${{LOG_LEVEL}}] (${{FILE}}:${{LINE}}) ${{STR}}\n"); -// } + + if (args.contains("--nbt")) + { + auto v = blt::arg_parse::get(args["nbt"]); + if (v.empty()) + v = "1048576"; + blt::tests::nbtFSTest(std::stoul(v)); + blt::tests::nbtWrite(); + blt::tests::nbtRead(); + } + // // auto* funy = new class_func; // super_func* virtual_funy = new class_func; @@ -244,70 +257,5 @@ int main(int argc, const char** argv) { // // BLT_INFO("STDDEV of # random values: %f", stdev); - { - std::fstream nbtFile("super_file.nbt", std::ios::out | std::ios::binary); - blt::fs::fstream_block_writer blockWriter(nbtFile); - blt::nbt::NBTWriter nbtWriter(blockWriter); - nbtWriter.write( - new blt::nbt::tag_compound( - "root", { - new blt::nbt::tag_byte("super_byte", 8), - new blt::nbt::tag_short("shortTest", 32767), - new blt::nbt::tag_compound( - "SEXY_COMPOUND", { - new blt::nbt::tag_list( - "my list", { - new blt::nbt::tag_long("", 1230), - new blt::nbt::tag_long("", 2), - new blt::nbt::tag_long("", 50340535), - new blt::nbt::tag_long("", 55), - new blt::nbt::tag_long("", 256), - new blt::nbt::tag_long("", 512), - new blt::nbt::tag_long("", 9999999999), - } - ), - new blt::nbt::tag_double("OMG IT'S A DOUBLE", 1320.04324), - new blt::nbt::tag_float("OMG IT'S A FLOAT", 12.04324), - new blt::nbt::tag_compound( - "Triple", { - new blt::nbt::tag_int("Test int", 32), - new blt::nbt::tag_byte_array( - "super array", { - 51, 23, 12, 04, 33, 53, 11, 22, 3, 93, 120 - } - ), - new blt::nbt::tag_string("I am a string", "I have stringy contents"), - new blt::nbt::tag_string("name", "Bananrama"), - new blt::nbt::tag_int_array( - "int array", { - 1230, 234023, 21300, 2309230, 2340230, 2, 1, 32, 3265, 12, 53, 123, 7, - 56, 12 - } - ), - new blt::nbt::tag_long_array( - "valid", { - 1230, 5320, 323200234402304, 230023, 23042034, 230230, 2301203, - 123010230, 12300123 - } - ) - } - ) - } - ) - } - )); - - blockWriter.flush(); - nbtFile.close(); - } - - std::fstream nbtInputFile("super_file.nbt", std::ios::in | std::ios::binary); - blt::fs::fstream_block_reader blockReader(nbtInputFile); - blt::nbt::NBTReader nbtReader(blockReader); - nbtReader.read(); - - auto shortTag = nbtReader.getTag("shortTest"); - BLT_TRACE("Got short: %d", shortTag->get()); - return 0; } \ No newline at end of file diff --git a/src/tests/nbt_tests.cpp b/src/tests/nbt_tests.cpp new file mode 100644 index 0000000..cca0bc6 --- /dev/null +++ b/src/tests/nbt_tests.cpp @@ -0,0 +1,160 @@ +/* + * Created by Brett on 11/08/23. + * Licensed under GNU General Public License V3.0 + * See LICENSE file for license detail + */ +#include "nbt_tests.h" +#include +#include +#include +#include +#include +#include + +void blt::tests::nbtFSBlockRead(size_t buff_size, size_t arr_size, size_t num_array, unsigned char** arr) +{ + +} + +void blt::tests::nbtFSBlockWrite(size_t buff_size, size_t arr_size, size_t num_array, unsigned char** arr) +{ + std::fstream out("fs_tests/test_block_fs_" + std::to_string(buff_size) + ".all", std::ios::out | std::ios::binary); + blt::fs::fstream_block_writer writer(out, buff_size); + + auto str = "fs::block::" + std::to_string(buff_size); + BLT_START_INTERVAL("Write Tests", str); + for (size_t _ = 0; _ < num_array; _++) + writer.write(reinterpret_cast(arr[_]), arr_size); + BLT_END_INTERVAL("Write Tests", str); +} + +void blt::tests::nbtFSRead(size_t buff_size, size_t arr_size, size_t num_array, unsigned char** arr) +{ + +} + +void blt::tests::nbtFSWrite(size_t buff_size, size_t arr_size, size_t num_array, unsigned char** arr) +{ + std::fstream out("fs_tests/test_fs_" + std::to_string(buff_size) + ".all", std::ios::out | std::ios::binary); + + auto str = "fs::std::" + std::to_string(buff_size); + BLT_START_INTERVAL("Write Tests", str); + for (size_t _ = 0; _ < num_array; _++) + out.write(reinterpret_cast(arr[_]), (long)arr_size); + BLT_END_INTERVAL("Write Tests", str); +} + +void blt::tests::nbtFSTest(size_t total_size) +{ + std::filesystem::create_directory("fs_tests/"); + for (int i = 10; i < 20; i++) + { + auto buff_size = (size_t) std::pow(2, i); + auto arr_size = 512 * 1; + auto num_array = (total_size) / arr_size; + + auto* arr = new unsigned char* [num_array]; + for (size_t _ = 0; _ < num_array; _++) + { + arr[_] = new unsigned char[arr_size]; + generateRandomData(arr[_], arr_size, _); + } + + nbtFSBlockWrite(buff_size, arr_size, num_array, arr); + nbtFSWrite(buff_size, arr_size, num_array, arr); + + for (size_t _ = 0; _ < num_array; _++) + delete[] arr[_]; + delete[] arr; + } + BLT_PRINT_PROFILE("Write Tests"); +} + +void blt::tests::nbtRawRead() +{ + +} + +void blt::tests::nbtRawWrite() +{ + +} + +void blt::tests::nbtRawTest() +{ + +} + +void blt::tests::nbtRead() +{ + std::fstream nbtInputFile("super_file.nbt", std::ios::in | std::ios::binary); + blt::fs::fstream_block_reader blockReader(nbtInputFile); + blt::nbt::NBTReader nbtReader(blockReader); + nbtReader.read(); + + auto shortTag = nbtReader.getTag("shortTest"); + BLT_TRACE("Got short: %d", shortTag->get()); +} + +void blt::tests::nbtWrite() +{ + std::fstream nbtFile("super_file.nbt", std::ios::out | std::ios::binary); + blt::fs::fstream_block_writer blockWriter(nbtFile); + blt::nbt::NBTWriter nbtWriter(blockWriter); + nbtWriter.write( + new blt::nbt::tag_compound( + "root", { + new blt::nbt::tag_byte("super_byte", 8), + new blt::nbt::tag_short("shortTest", 32767), + new blt::nbt::tag_compound( + "SEXY_COMPOUND", { + new blt::nbt::tag_list( + "my list", { + new blt::nbt::tag_long("", 1230), + new blt::nbt::tag_long("", 2), + new blt::nbt::tag_long("", 50340535), + new blt::nbt::tag_long("", 55), + new blt::nbt::tag_long("", 256), + new blt::nbt::tag_long("", 512), + new blt::nbt::tag_long("", 9999999999), + } + ), + new blt::nbt::tag_double("OMG IT'S A DOUBLE", 1320.04324), + new blt::nbt::tag_float("OMG IT'S A FLOAT", 12.04324), + new blt::nbt::tag_compound( + "Triple", { + new blt::nbt::tag_int("Test int", 32), + new blt::nbt::tag_byte_array( + "super array", { + 51, 23, 12, 04, 33, 53, 11, 22, 3, 93, 120 + } + ), + new blt::nbt::tag_string("I am a string", "I have stringy contents"), + new blt::nbt::tag_string("name", "Bananrama"), + new blt::nbt::tag_int_array( + "int array", { + 1230, 234023, 21300, 2309230, 2340230, 2, 1, 32, 3265, 12, 53, 123, 7, + 56, 12 + } + ), + new blt::nbt::tag_long_array( + "valid", { + 1230, 5320, 323200234402304, 230023, 23042034, 230230, 2301203, + 123010230, 12300123 + } + ) + } + ) + } + ) + } + )); + + blockWriter.flush(); + nbtFile.close(); +} + +void blt::tests::nbtTest() +{ + +} diff --git a/src/tests/nbt_tests.h b/src/tests/nbt_tests.h index 97afd02..187023d 100755 --- a/src/tests/nbt_tests.h +++ b/src/tests/nbt_tests.h @@ -7,214 +7,35 @@ #ifndef BLT_TESTS_NBT_TESTS_H #define BLT_TESTS_NBT_TESTS_H -#include -#include -#include -#include -#include +#include +#include -inline bool readLargeBlockUsingNBTBufferedReader(const std::string& file, const blt::scoped_buffer& bufferToCompare, size_t bufferSize) { - blt::scoped_buffer read_buffer{bufferToCompare.size()}; - std::fstream largeBlockInputLarge(file, std::ios::in | std::ios::binary); - blt::fs::fstream_block_reader byteLargeBlockInputLarge(largeBlockInputLarge, bufferSize); - byteLargeBlockInputLarge.read(*read_buffer, bufferToCompare.size()); - for (unsigned int i = 0; i < bufferToCompare.size(); i++) { - if (read_buffer[i] != bufferToCompare[i]) - return false; - } - largeBlockInputLarge.close(); - return true; -} - -inline bool readIndividualUsingNBTBufferedReader(const std::string& file, const blt::scoped_buffer& bufferToCompare, size_t bufferSize) { - std::fstream largeBlockInput(file, std::ios::in | std::ios::binary); - blt::fs::fstream_block_reader byteLargeBlockInput(largeBlockInput, bufferSize); - for (unsigned int i = 0; i < bufferToCompare.size(); i++) { - char byte; - byteLargeBlockInput.read(&byte, 1); - if (byte != bufferToCompare[i]) { - return false; - } - } - largeBlockInput.close(); - return true; -} - -inline void nbt_read_tests(){ - constexpr auto bufferSize = 1024 * 1024; +namespace blt::tests { - blt::scoped_buffer buffer{bufferSize}; - - char* read_buffer = new char[bufferSize]; - char* read_block_buffer = new char[bufferSize]; - - bool fstream_indv_correct = true; - bool fstream_large_correct = true; - - for (int i = 0; i < bufferSize; i++) - buffer[i] = i + 1; - - BLT_START_INTERVAL("nbt read", "Raw Write"); - std::fstream largeOutput("HeyThere.txt", std::ios::out | std::ios::binary); - largeOutput.write(*buffer, bufferSize); - largeOutput.flush(); - largeOutput.close(); - BLT_END_INTERVAL("nbt read", "Raw Write"); - - BLT_START_INTERVAL("nbt read", "Raw Read Individual"); - std::fstream largeInput("HeyThere.txt", std::ios::in | std::ios::binary); - for (int i = 0; i < bufferSize; i++) { - char byte; - largeInput.read(&byte, 1); - if (byte != buffer[i]) { - fstream_indv_correct = false; - break; - } - } - largeInput.close(); - BLT_END_INTERVAL("nbt read", "Raw Read Individual"); - - BLT_START_INTERVAL("nbt read", "Raw Read Large"); - std::fstream largeInputLarge("HeyThere.txt", std::ios::in | std::ios::binary); - largeInputLarge.read(read_buffer, bufferSize); - for (int i = 0; i < bufferSize; i++) { - if (read_buffer[i] != buffer[i]) - fstream_large_correct = false; - } - largeInputLarge.close(); - BLT_END_INTERVAL("nbt read", "Raw Read Large"); - - BLT_INFO("FStream Read Correctly? %s;", fstream_indv_correct ? "True" : "False"); - BLT_INFO("FStream Large Read Correctly? %s;", fstream_large_correct ? "True" : "False"); - - - for (int i = 0; i < 8; i++) { - auto size = (size_t) std::pow(2, 11 + i); - auto size_str = std::to_string(size); - auto profiler_string = "Block Read Individual " + size_str; - bool nbt_block_correct = true; - BLT_START_INTERVAL("nbt read individual", profiler_string); - nbt_block_correct = readIndividualUsingNBTBufferedReader("HeyThere.txt", buffer, size); - BLT_END_INTERVAL("nbt read individual", profiler_string); + template + T* generateRandomData(T* arr, size_t size, uint32_t seed = 0) { + for (size_t i = 0; i < size; i++) + arr[i] = blt::random::randomInt_c(i * size + seed, std::numeric_limits::min(), std::numeric_limits::max()); - BLT_INFO("NBT Individual Block %s Stream Correctly? %s;\n", size_str.c_str(), nbt_block_correct ? "True" : "False"); + return arr; } - for (int i = 0; i < 8; i++) { - auto size = (size_t) std::pow(2, 11 + i); - auto size_str = std::to_string(size); - auto profiler_string = "Block Read " + size_str; - bool nbt_block_large_correct = true; - BLT_START_INTERVAL("nbt read block", profiler_string); - nbt_block_large_correct = readLargeBlockUsingNBTBufferedReader("HeyThere.txt", buffer, size); - BLT_END_INTERVAL("nbt read block", profiler_string); - - BLT_INFO("NBT Block %s Stream Correctly? %s;\n", size_str.c_str(), nbt_block_large_correct ? "True" : "False"); - } + // test block fs vs std::ios + void nbtFSBlockRead(size_t buff_size, size_t arr_size, size_t num_array, unsigned char** arr); + void nbtFSBlockWrite(size_t buff_size, size_t arr_size, size_t num_array, unsigned char** arr); + void nbtFSRead(size_t buff_size, size_t arr_size, size_t num_array, unsigned char** arr); + void nbtFSWrite(size_t buff_size, size_t arr_size, size_t num_array, unsigned char** arr); + void nbtFSTest(size_t total_size); - BLT_PRINT_PROFILE("nbt read"); - BLT_TRACE("{BLANK_LINE}"); - BLT_PRINT_PROFILE("nbt read block"); - BLT_TRACE("{BLANK_LINE}"); - BLT_PRINT_PROFILE("nbt read individual"); - - delete[] read_buffer; - delete[] read_block_buffer; -} - -inline void nbt_write_tests(){ - constexpr auto bufferSize = 1024 * 1024; - - blt::scoped_buffer buffer{bufferSize}; - blt::scoped_buffer read_buffer{bufferSize}; - - for (int i = 0; i < bufferSize; i++) - buffer[i] = i + 1; - - std::fstream fileOutput("IAmAFile.txt", std::ios::binary | std::ios::out); - for (int i = 0; i < 8; i++) { - auto size = (size_t) std::pow(2, 11 + i); - auto size_str = std::to_string(size); - auto profiler_string = "Writer " + size_str; - blt::fs::fstream_block_writer writer(fileOutput, size); - - BLT_START_INTERVAL("nbt write block", profiler_string); - writer.write(*buffer, buffer.size()); - BLT_END_INTERVAL("nbt write block", profiler_string); - BLT_START_INTERVAL("nbt write individual", profiler_string); - for (int j = 0; j < bufferSize; j++) { - writer.write(&buffer[j], 1); - } - BLT_END_INTERVAL("nbt write individual", profiler_string); - } - fileOutput.flush(); - - std::fstream fileInput("IAmAFile.txt", std::ios::binary | std::ios::in); - - for (int i = 0; i < 8; i++) { - auto size = (size_t) std::pow(2, 11 + i); - auto size_str = std::to_string(size); - bool results = true; - fileInput.read(*read_buffer, bufferSize); - for (int j = 0; j < bufferSize; j++) { - if (buffer[j] != read_buffer[j]) { - results = false; - BLT_FATAL("Error occurred at size %d and index %d", size, j); - break; - } - } - BLT_INFO("NBT %s Block Write Correctly? %s;\n", size_str.c_str(), results ? "True" : "False"); - - results = true; - fileInput.read(*read_buffer, bufferSize); - for (int j = 0; j < bufferSize; j++) { - if (buffer[j] != read_buffer[j]) { - results = false; - BLT_FATAL("Error occurred at size %d and index %d", size, j); - break; - } - } - BLT_INFO("NBT %s Individual Write Correctly? %s;\n", size_str.c_str(), results ? "True" : "False"); - } - - BLT_PRINT_PROFILE("nbt write individual"); - BLT_TRACE(""); - BLT_PRINT_PROFILE("nbt write block"); -} - -inline void nbt_tests(){ - std::fstream testOutput("Hello.txt", std::ios::out | std::ios::binary); - - char testByte[] = {3, 'A', 91}; - short testShort = 6132; - int testInt = 6600099; - - - testOutput.write(testByte, 3); - testOutput.write(reinterpret_cast(&testShort), sizeof(short)); - testOutput.write(reinterpret_cast(&testInt), sizeof(int)); - //blt::nbt::writeUTF8String(testOutput, "HelloHowManyCanWeFit!"); - - //testOutput.flush(); - testOutput.close(); - - std::fstream testInput("Hello.txt", std::ios::in | std::ios::binary); - - char testByteIn[3]; - short testShortIn; - int testIntIn; - - testInput.read(testByteIn, 3); - testInput.read(reinterpret_cast(&testShortIn), sizeof(short)); - testInput.read(reinterpret_cast(&testIntIn), sizeof(int)); - //std::string strIn = blt::nbt::readUTF8String(testInput); - - testInput.close(); - //BLT_INFO("%d, %c, %d, %d, %d, %s", testByteIn[0], testByteIn[1], testByteIn[2], testShortIn, testIntIn, strIn.c_str()); - - nbt_read_tests(); - nbt_write_tests(); + // test raw ios r/w speeds + void nbtRawRead(); + void nbtRawWrite(); + void nbtRawTest(); + // test nbt r/w speeds + void nbtRead(); + void nbtWrite(); + void nbtTest(); } #endif //BLT_TESTS_NBT_TESTS_H