[binaryToString] Add unit tests

This commit is contained in:
Dan Čermák 2018-09-30 00:28:34 +02:00
parent 7a7ae7a1df
commit 30787e6f1e
2 changed files with 49 additions and 0 deletions

View File

@ -11,6 +11,7 @@ add_executable(unit_tests mainTestRunner.cpp
test_cr2header_int.cpp test_cr2header_int.cpp
test_helper_functions.cpp test_helper_functions.cpp
test_slice.cpp test_slice.cpp
test_image_int.cpp
$<TARGET_OBJECTS:exiv2lib_int> $<TARGET_OBJECTS:exiv2lib_int>
) )

View File

@ -0,0 +1,48 @@
#include "gtestwrapper.h"
#include <image_int.hpp>
using namespace Exiv2::Internal;
static const unsigned char buf[10] = {'a', 'b', 'c', 1, 4, 0, 'e', 136, 0, 'a'};
TEST(binaryToString, zeroStart)
{
// a, b, c are printable, 1 & 4 are not => '.', 0 at last position => skipped
ASSERT_STREQ(binaryToString(buf, 6, 0).c_str(), "abc..");
// same as previous, but now last element is not ignored since it is not 0
ASSERT_STREQ(binaryToString(buf, 5, 0).c_str(), "abc..");
// same as first, only now the 0 & 136 are converted to '.'
ASSERT_STREQ(binaryToString(buf, 8, 0).c_str(), "abc...e.");
// should result in the same as previously, as trailing zero is ignored
ASSERT_STREQ(binaryToString(buf, 9, 0).c_str(), "abc...e.");
// ensure that the function does not overread when last element != 0
ASSERT_STREQ(binaryToString(buf, sizeof(buf), 0).c_str(), "abc...e..a");
}
TEST(binaryToString, nonZeroStart)
{
// start @ index 1, read 6 characters (until e)
ASSERT_STREQ(binaryToString(buf, 6, 1).c_str(), "bc...e");
// start @ index 3, read until end
ASSERT_STREQ(binaryToString(buf, sizeof(buf) - 3, 3).c_str(), "...e..a");
}
TEST(binaryToString, DataBuf)
{
const Exiv2::DataBuf data_buf(buf, sizeof(buf));
// same as first case in zeroStart
ASSERT_EQ(binaryToString(data_buf, 6, 0), "abc..");
// try to pass a too large value for size
ASSERT_EQ(binaryToString(data_buf, 200, 0), "abc...e..a");
// make it blow up by setting start larger than zero...
ASSERT_EQ(binaryToString(data_buf, 200, 1), "bc...e..a");
}