add header for data and move there common consts

This commit is contained in:
Alexandra Latysheva 2020-10-28 14:04:14 +00:00
parent b74b3c80fc
commit ce12cac60c
7 changed files with 55 additions and 95 deletions

View File

@ -15,6 +15,9 @@
add_executable(sandboxed add_executable(sandboxed
main_sandboxed.cc main_sandboxed.cc
../sandboxed.h ../sandboxed.h
../test/data.h
../test/helper.h
../test/helper.cc
) )
target_link_libraries(sandboxed PRIVATE target_link_libraries(sandboxed PRIVATE

View File

@ -12,12 +12,11 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include <array>
#include <cstdint>
#include <cstdlib> #include <cstdlib>
#include <cstring>
#include "../sandboxed.h" // NOLINT(build/include) #include "../sandboxed.h" // NOLINT(build/include)
#include "../test/data.h" // NOLINT(build/include)
#include "../test/helper.h" // NOLINT(build/include)
#include "absl/algorithm/container.h" #include "absl/algorithm/container.h"
#include "absl/strings/str_join.h" #include "absl/strings/str_join.h"
#include "sandboxed_api/sandbox2/util/fileops.h" #include "sandboxed_api/sandbox2/util/fileops.h"
@ -27,35 +26,6 @@
namespace { namespace {
struct ChannelLimits {
uint8_t min_red;
uint8_t max_red;
uint8_t min_green;
uint8_t max_green;
uint8_t min_blue;
uint8_t max_blue;
uint8_t min_alpha;
uint8_t max_alpha;
};
constexpr uint32_t kRawTileNumber = 9;
constexpr uint32_t kClusterSize = 6;
constexpr uint32_t kChannelsInPixel = 3;
constexpr uint32_t kTestCount = 3;
constexpr uint32_t kImageSize = 128 * 128;
constexpr uint32_t kClusterImageSize = 64 * 64;
using ClusterData = std::array<uint8_t, kClusterSize>;
constexpr std::array<std::pair<uint32_t, ClusterData>, kTestCount> kClusters = {
{{0, {0, 0, 2, 0, 138, 139}},
{64, {0, 0, 9, 6, 134, 119}},
{128, {44, 40, 63, 59, 230, 95}}}};
constexpr std::array<std::pair<uint32_t, ChannelLimits>, kTestCount> kLimits = {
{{0, {15, 18, 0, 0, 18, 41, 255, 255}},
{64, {0, 0, 0, 0, 0, 2, 255, 255}},
{512, {5, 6, 34, 36, 182, 196, 255, 255}}}};
constexpr absl::string_view kClusterErrorFormatStr = constexpr absl::string_view kClusterErrorFormatStr =
"Cluster %d did not match expected results.\n" "Cluster %d did not match expected results.\n"
"Expect:\t%s\n" "Expect:\t%s\n"
@ -141,30 +111,6 @@ absl::Status CheckRgbaPixel(uint32_t pixel, const ChannelLimits& limits,
} // namespace } // namespace
std::string GetFilePath(const absl::string_view dir,
const absl::string_view filename) {
return sandbox2::file::JoinPath(dir, "test", "images", filename);
}
std::string GetFilePath(const absl::string_view filename) {
std::string cwd = sandbox2::file_util::fileops::GetCWD();
auto find = cwd.rfind("build");
std::string project_path;
if (find == std::string::npos) {
LOG(ERROR)
<< "Something went wrong: CWD don't contain build dir. "
<< "Please run tests from build dir or send project dir as a "
<< "parameter: ./sandboxed /absolute/path/to/project/dir .\n"
<< "Falling back to using current working directory as root dir.\n";
project_path = cwd;
} else {
project_path = cwd.substr(0, find);
}
return sandbox2::file::JoinPath(project_path, "test", "images", filename);
}
absl::Status LibTIFFMain(const std::string& srcfile) { absl::Status LibTIFFMain(const std::string& srcfile) {
// to use dir and file inside sapi-libtiff, use // to use dir and file inside sapi-libtiff, use
// sandbox(file) file only -- or // sandbox(file) file only -- or

View File

@ -15,6 +15,7 @@
include(GoogleTest) include(GoogleTest)
add_executable(tests add_executable(tests
../test/data.h
check_tag.h check_tag.h
check_tag.cc check_tag.cc
defer_strile_writing.cc defer_strile_writing.cc

View File

@ -0,0 +1,31 @@
#include <array>
constexpr uint32_t kRawTileNumber = 9;
constexpr uint32_t kClusterSize = 6;
constexpr uint32_t kChannelsInPixel = 3;
constexpr uint32_t kTestCount = 3;
constexpr uint32_t kImageSize = 128 * 128;
constexpr uint32_t kClusterImageSize = 64 * 64;
using ClusterData = std::array<uint8_t, kClusterSize>;
struct ChannelLimits {
uint8_t min_red;
uint8_t max_red;
uint8_t min_green;
uint8_t max_green;
uint8_t min_blue;
uint8_t max_blue;
uint8_t min_alpha;
uint8_t max_alpha;
};
constexpr std::array<std::pair<uint32_t, ClusterData>, kTestCount> kClusters = {
{{0, {0, 0, 2, 0, 138, 139}},
{64, {0, 0, 9, 6, 134, 119}},
{128, {44, 40, 63, 59, 230, 95}}}};
constexpr std::array<std::pair<uint32_t, ChannelLimits>, kTestCount> kLimits = {
{{0, {15, 18, 0, 0, 18, 41, 255, 255}},
{64, {0, 0, 0, 0, 0, 2, 255, 255}},
{512, {5, 6, 34, 36, 182, 196, 255, 255}}}};

View File

@ -18,20 +18,24 @@
#include "sandboxed_api/sandbox2/util/fileops.h" #include "sandboxed_api/sandbox2/util/fileops.h"
#include "sandboxed_api/sandbox2/util/path.h" #include "sandboxed_api/sandbox2/util/path.h"
std::string GetImagesFolder() { std::string GetFilePath(const absl::string_view filename) {
std::string cwd = sandbox2::file_util::fileops::GetCWD(); std::string cwd = sandbox2::file_util::fileops::GetCWD();
auto find = cwd.rfind("/build"); auto find = cwd.rfind("/build");
if (find == std::string::npos) { if (find == std::string::npos) {
LOG(ERROR) << "Something went wrong: CWD don't contain build dir. " LOG(ERROR)
<< "Please run tests from build dir, path might be incorrect\n"; << "Something went wrong: CWD don't contain build dir. Please run "
"tests from build dir. To run example send project dir as a "
"parameter: ./sandboxed /absolute/path/to/project/dir .\n"
"Falling back to using current working directory as root dir.\n";
return sandbox2::file::JoinPath(cwd, "test", "images"); return sandbox2::file::JoinPath(cwd, "test", "images");
} }
return sandbox2::file::JoinPath(cwd.substr(0, find), "test", "images"); return sandbox2::file::JoinPath(cwd.substr(0, find), "test", "images",
filename);
} }
std::string GetFilePath(const std::string& filename) { std::string GetFilePath(const absl::string_view dir,
std::string images_folder_path = GetImagesFolder(); const absl::string_view filename) {
return sandbox2::file::JoinPath(images_folder_path, filename); return sandbox2::file::JoinPath(dir, "test", "images", filename);
} }

View File

@ -14,4 +14,8 @@
#include <string> #include <string>
std::string GetFilePath(const std::string& filename); #include "absl/strings/str_join.h"
std::string GetFilePath(const absl::string_view filename);
std::string GetFilePath(const absl::string_view dir,
const absl::string_view filename);

View File

@ -12,10 +12,10 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include <array>
#include <cstring> #include <cstring>
#include "../sandboxed.h" // NOLINT(build/include) #include "../sandboxed.h" // NOLINT(build/include)
#include "../test/data.h" // NOLINT(build/include)
#include "absl/algorithm/container.h" #include "absl/algorithm/container.h"
#include "absl/strings/str_join.h" #include "absl/strings/str_join.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
@ -35,35 +35,6 @@ using ::testing::IsTrue;
using ::testing::Le; using ::testing::Le;
using ::testing::NotNull; using ::testing::NotNull;
struct ChannelLimits {
uint8_t min_red;
uint8_t max_red;
uint8_t min_green;
uint8_t max_green;
uint8_t min_blue;
uint8_t max_blue;
uint8_t min_alpha;
uint8_t max_alpha;
};
constexpr uint32_t kRawTileNumber = 9;
constexpr uint32_t kClusterSize = 6;
constexpr uint32_t kChannelsInPixel = 3;
constexpr uint32_t kTestCount = 3;
constexpr uint32_t kImageSize = 128 * 128;
constexpr uint32_t kClusterImageSize = 64 * 64;
using ClusterData = std::array<uint8_t, kClusterSize>;
constexpr std::array<std::pair<uint32_t, ClusterData>, kTestCount> kClusters = {
{{0, {0, 0, 2, 0, 138, 139}},
{64, {0, 0, 9, 6, 134, 119}},
{128, {44, 40, 63, 59, 230, 95}}}};
constexpr std::array<std::pair<uint32_t, ChannelLimits>, kTestCount> kLimits = {
{{0, {15, 18, 0, 0, 18, 41, 255, 255}},
{64, {0, 0, 0, 0, 0, 2, 255, 255}},
{512, {5, 6, 34, 36, 182, 196, 255, 255}}}};
void CheckCluster(uint32_t cluster, const sapi::v::Array<uint8_t>& buffer, void CheckCluster(uint32_t cluster, const sapi::v::Array<uint8_t>& buffer,
const ClusterData& expected_cluster) { const ClusterData& expected_cluster) {
ASSERT_THAT(buffer.GetSize(), Ge((cluster + 1) * kClusterSize)) << "Overrun"; ASSERT_THAT(buffer.GetSize(), Ge((cluster + 1) * kClusterSize)) << "Overrun";