diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/CMakeLists.txt | 9 | ||||
| -rw-r--r-- | test/format_test.cpp | 21 | ||||
| -rw-r--r-- | test/test_value.cpp | 34 |
3 files changed, 62 insertions, 2 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 234b0cf9d2..41549842cb 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -107,3 +107,12 @@ add_executable(format_test format_test.cpp) target_link_libraries(format_test ${NBT_NAME}) add_test(format_test format_test) stop_warnings(format_test) + +# Optional local test executable to verify value assignments (developer helper) +option(NBT_BUILD_LOCAL_TEST "Build a small local test executable for value assignments" ON) +if(NBT_BUILD_LOCAL_TEST) + add_executable(test_value test_value.cpp) + target_link_libraries(test_value ${NBT_NAME}) + add_test(test_value test_value) + stop_warnings(test_value) +endif() diff --git a/test/format_test.cpp b/test/format_test.cpp index 559af11299..1a689ed3f2 100644 --- a/test/format_test.cpp +++ b/test/format_test.cpp @@ -22,7 +22,8 @@ * along with libnbt++. If not, see <http://www.gnu.org/licenses/>. */ //#include "text/json_formatter.h" -//#include "io/stream_reader.h" +#include "io/stream_reader.h" +#include "io/stream_writer.h" #include <fstream> #include <iostream> #include <limits> @@ -32,7 +33,7 @@ using namespace nbt; int main() { - //TODO: Write that into a file + // Write that into a file and read back for testing tag_compound comp{ {"byte", tag_byte(-128)}, {"short", tag_short(-32768)}, @@ -83,4 +84,20 @@ int main() std::cout << "----- default operator<<:\n"; std::cout << comp; std::cout << "\n-----" << std::endl; + + // Write to file and read back + { + std::ofstream out("test_output.nbt", std::ios::binary); + nbt::io::write_compound(out, comp); + } + + { + std::ifstream in("test_output.nbt", std::ios::binary); + auto [read_comp, name] = nbt::io::read_compound(in); + std::cout << "----- read back from file:\n"; + std::cout << read_comp; + std::cout << "\n-----" << std::endl; + } + + return 0; } diff --git a/test/test_value.cpp b/test/test_value.cpp new file mode 100644 index 0000000000..009cefe8ef --- /dev/null +++ b/test/test_value.cpp @@ -0,0 +1,34 @@ +#include <iostream> +#include "nbt_tags.h" +#include "value.h" + +using namespace nbt; + +int main() +{ + try { + value v; + + v = int8_t(-5); + std::cout << "assigned int8_t(-5): as int32=" << int32_t(v) << ", as double=" << double(v) << "\n"; + + v = int16_t(12345); + std::cout << "assigned int16_t(12345): as int32=" << int32_t(v) << ", as double=" << double(v) << "\n"; + + v = int32_t(100000); + std::cout << "assigned int32_t(100000): as int64=" << int64_t(v) << ", as double=" << double(v) << "\n"; + + v = float(3.14f); + std::cout << "assigned float(3.14): as double=" << double(v) << "\n"; + + v = double(2.718281828); + std::cout << "assigned double(2.71828): as double=" << double(v) << "\n"; + + std::cout << "Test finished OK\n"; + } + catch(const std::exception& e) { + std::cerr << "Exception: " << e.what() << "\n"; + return 1; + } + return 0; +} |
