-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libnixt/serialize: add unit tests for ExprInt and ExprFloat
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include "nixt/Serialize.h" | ||
|
||
#include <nix/nixexpr.hh> | ||
|
||
#include <bit> | ||
|
||
using namespace nixt::serialize; | ||
|
||
static_assert(std::endian::native == std::endian::little); | ||
|
||
namespace { | ||
|
||
class SerializeTest : public testing::Test { | ||
protected: | ||
nix::PosTable PT; | ||
nix::SymbolTable ST; | ||
std::ostringstream OS; | ||
std::string write(const nix::Expr *E) { | ||
nixt::serialize::write(OS, ST, PT, E); | ||
return OS.str(); | ||
} | ||
}; | ||
|
||
void checkASTHeader(const ASTHeader &H) { | ||
EXPECT_EQ(std::memcmp(H.Magic, "\x7FNixAST\0", 8), 0); | ||
EXPECT_EQ(H.Version, 1); | ||
} | ||
|
||
void checkASTHeader(const char *Raw) { | ||
ASTHeader Header; | ||
std::memcpy(&Header, Raw, sizeof(ASTHeader)); | ||
checkASTHeader(Header); | ||
} | ||
|
||
} // namespace | ||
|
||
TEST_F(SerializeTest, ExprInt) { | ||
nix::ExprInt E(0x2adeadbeef); | ||
std::string Result = write(&E); | ||
const char Expected[] = "\x6\0\0\0\xEF\xBE\xAD\xDE\x2A\0\0"; | ||
checkASTHeader(Result.c_str()); | ||
auto Content = std::string_view(Result).substr(sizeof(ASTHeader)); | ||
EXPECT_TRUE(std::memcmp(Expected, Content.begin(), sizeof(Expected)) == 0); | ||
EXPECT_EQ(Content.size(), sizeof(Expected)); | ||
} | ||
|
||
TEST_F(SerializeTest, ExprFloat) { | ||
union { | ||
double D; | ||
std::uint64_t U; | ||
} Data; | ||
Data.U = 0x2adeadbeef; | ||
|
||
nix::ExprFloat E(Data.D); | ||
std::string Result = write(&E); | ||
const char Expected[] = "\x4\0\0\0\xEF\xBE\xAD\xDE\x2A\0\0"; | ||
checkASTHeader(Result.c_str()); | ||
auto Content = std::string_view(Result).substr(sizeof(ASTHeader)); | ||
EXPECT_TRUE(std::memcmp(Expected, Content.begin(), sizeof(Expected)) == 0); | ||
EXPECT_EQ(Content.size(), sizeof(Expected)); | ||
} |