Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple unit tests for binary files #826

Merged
merged 7 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Correct training with fp16 via `--fp16`.
- Dynamic cost-scaling with `--cost-scaling`.
- Dynamic gradient-scaling with `--dynamic-gradient-scaling`.
- Add unit tests for binary files.
- Fix compilation with OMP

### Fixed
Expand Down
1 change: 1 addition & 0 deletions src/common/file_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ void TemporaryFile::MakeTemp(const std::string &base) {
int fd = open(name, oflag, _S_IREAD | _S_IWRITE);
ABORT_IF(fd == -1, "Error while making a temporary based on '{}'", base);

file_ = std::string(name);
#else
// create temp file
std::string name(base);
Expand Down
1 change: 1 addition & 0 deletions src/tests/units/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(UNIT_TESTS
attention_tests
fastopt_tests
utils_tests
binary_tests
# cosmos_tests # optional, uncomment to test with specific files.
)

Expand Down
61 changes: 61 additions & 0 deletions src/tests/units/binary_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "catch.hpp"
#include "common/binary.h"
#include "common/file_stream.h"

#include "3rd_party/mio/mio.hpp"

using namespace marian;

TEST_CASE("a few operations on binary files", "[binary]") {

SECTION("Save two items to temporary binary file and then load and map") {

// Create a temporary file that we will only use for the file name
io::TemporaryFile temp("/tmp/", /*earlyUnlink=*/false);
io::Item item1, item2;

{
std::vector<float> v1 = { 3.14, 2.71, 1.0, 0.0, 1.41 };
std::vector<uint16_t> v2 = { 5, 4, 3, 2, 1, 0 };

item1.name = "item1";
item1.shape = { 5, 1 };
item1.type = Type::float32;
item1.bytes.resize(v1.size() * sizeof(float));
std::copy((char*)v1.data(), (char*)v1.data() + v1.size() * sizeof(float), item1.bytes.data());

item2.name = "item2";
item2.shape = { 2, 3 };
item2.type = Type::uint16;
item2.bytes.resize(v2.size() * sizeof(uint32_t));
std::copy((char*)v2.data(), (char*)v2.data() + v2.size() * sizeof(uint16_t), item2.bytes.data());

std::vector<io::Item> items = {item1, item2};
io::binary::saveItems(temp.getFileName(), items);
}

{ // test loading
std::vector<io::Item> items;
io::binary::loadItems(temp.getFileName(), items);

CHECK( item1.name == items[0].name );
CHECK( item2.name == items[1].name );

CHECK( std::equal(item1.data(), item1.data() + item1.size(), items[0].data()) );
CHECK( std::equal(item2.data(), item2.data() + item2.size(), items[1].data()) );
}

{ // test mmapping
mio::mmap_source mmap(temp.getFileName());

std::vector<io::Item> items;
io::binary::loadItems(mmap.data(), items, /*mapped=*/true);

CHECK( item1.name == items[0].name );
CHECK( item2.name == items[1].name );

CHECK( std::equal(item1.data(), item1.data() + item1.size(), items[0].data()) );
CHECK( std::equal(item2.data(), item2.data() + item2.size(), items[1].data()) );
}
}
}