Applied requested changes

This commit is contained in:
Andrei Medar 2020-09-14 15:23:29 +00:00
parent c562f818c6
commit 0bc5c28cb5
5 changed files with 43 additions and 40 deletions

View File

@ -15,15 +15,18 @@
#include "helpers.h" #include "helpers.h"
std::vector<uint8_t> GenerateValues() { std::vector<uint8_t> GenerateValues() {
std::vector<uint8_t> image(kImgLen); std::vector<uint8_t> image;
image.reserve(kImgLen);
for (int y = 0; y < kHeight; ++y) { for (int y = 0; y < kHeight; ++y) {
for (int x = 0; x < kWidth; ++x) { for (int x = 0; x < kWidth; ++x) {
image[4 * kWidth * y + 4 * x + 0] = 255 * !(x & y); image.push_back(255 * !(x & y));
image[4 * kWidth * y + 4 * x + 1] = x ^ y; image.push_back(x ^ y);
image[4 * kWidth * y + 4 * x + 2] = x | y; image.push_back(x | y);
image[4 * kWidth * y + 4 * x + 3] = 255; image.push_back(255);
} }
} }
return image; return image;
} }

View File

@ -22,9 +22,9 @@
#include "sandboxed_api/sandbox2/util/fileops.h" #include "sandboxed_api/sandbox2/util/fileops.h"
#include "sandboxed_api/sandbox2/util/temp_file.h" #include "sandboxed_api/sandbox2/util/temp_file.h"
constexpr uint32_t kWidth = 512; constexpr size_t kWidth = 512;
constexpr uint32_t kHeight = 512; constexpr size_t kHeight = 512;
constexpr uint32_t kImgLen = kWidth * kHeight * 4; constexpr size_t kImgLen = kWidth * kHeight * 4;
// Returns a vector that contains values used for testing. // Returns a vector that contains values used for testing.
// This part of code is taken from // This part of code is taken from

View File

@ -21,7 +21,7 @@
void EncodeDecodeOneStep(SapiLodepngSandbox &sandbox, LodepngApi &api) { void EncodeDecodeOneStep(SapiLodepngSandbox &sandbox, LodepngApi &api) {
// Generate the values. // Generate the values.
std::vector<uint8_t> image(GenerateValues()); std::vector<uint8_t> image = GenerateValues();
// Encode the image. // Encode the image.
sapi::v::Array<uint8_t> sapi_image(kImgLen); sapi::v::Array<uint8_t> sapi_image(kImgLen);
@ -38,18 +38,18 @@ void EncodeDecodeOneStep(SapiLodepngSandbox &sandbox, LodepngApi &api) {
// After the image has been encoded, decode it to check that the // After the image has been encoded, decode it to check that the
// pixel values are the same. // pixel values are the same.
sapi::v::UInt sapi_width2, sapi_height2; sapi::v::UInt sapi_width, sapi_height;
sapi::v::IntBase<uint8_t *> sapi_image_ptr(0); sapi::v::IntBase<uint8_t *> sapi_image_ptr(0);
result = api.lodepng_decode32_file( result = api.lodepng_decode32_file(
sapi_image_ptr.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(), sapi_image_ptr.PtrBoth(), sapi_width.PtrBoth(), sapi_height.PtrBoth(),
sapi_filename.PtrBefore()); sapi_filename.PtrBefore());
CHECK(result.ok()) << "decode32_file call failes"; CHECK(result.ok()) << "decode32_file call failes";
CHECK(!result.value()) << "Unexpected result from decode32_file call"; CHECK(!result.value()) << "Unexpected result from decode32_file call";
CHECK(sapi_width2.GetValue() == kWidth) << "Widths differ"; CHECK(sapi_width.GetValue() == kWidth) << "Widths differ";
CHECK(sapi_height2.GetValue() == kHeight) << "Heights differ"; CHECK(sapi_height.GetValue() == kHeight) << "Heights differ";
// The pixels have been allocated inside the sandboxed process // The pixels have been allocated inside the sandboxed process
// memory, so we need to transfer them to this process. // memory, so we need to transfer them to this process.
@ -77,7 +77,7 @@ void EncodeDecodeOneStep(SapiLodepngSandbox &sandbox, LodepngApi &api) {
void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) { void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
// Generate the values. // Generate the values.
std::vector<uint8_t> image(GenerateValues()); std::vector<uint8_t> image = GenerateValues();
// Encode the image into memory first. // Encode the image into memory first.
sapi::v::Array<uint8_t> sapi_image(kImgLen); sapi::v::Array<uint8_t> sapi_image(kImgLen);
@ -115,7 +115,7 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
CHECK(!result.value()) << "Unexpected result from save_file call"; CHECK(!result.value()) << "Unexpected result from save_file call";
// Now, decode the image using the 2 steps in order to compare the values. // Now, decode the image using the 2 steps in order to compare the values.
sapi::v::UInt sapi_width2, sapi_height2; sapi::v::UInt sapi_width, sapi_height;
sapi::v::IntBase<uint8_t *> sapi_png_ptr2(0); sapi::v::IntBase<uint8_t *> sapi_png_ptr2(0);
sapi::v::ULLong sapi_pngsize2; sapi::v::ULLong sapi_pngsize2;
@ -141,14 +141,14 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
// directly. // directly.
sapi::v::IntBase<uint8_t *> sapi_png_ptr3(0); sapi::v::IntBase<uint8_t *> sapi_png_ptr3(0);
result = api.lodepng_decode32( result = api.lodepng_decode32(
sapi_png_ptr3.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(), sapi_png_ptr3.PtrBoth(), sapi_width.PtrBoth(), sapi_height.PtrBoth(),
sapi_png_array2.PtrBefore(), sapi_pngsize2.GetValue()); sapi_png_array2.PtrBefore(), sapi_pngsize2.GetValue());
CHECK(result.ok()) << "decode32 call failed"; CHECK(result.ok()) << "decode32 call failed";
CHECK(!result.value()) << "Unexpected result from decode32 call"; CHECK(!result.value()) << "Unexpected result from decode32 call";
CHECK(sapi_width2.GetValue() == kWidth) << "Widths differ"; CHECK(sapi_width.GetValue() == kWidth) << "Widths differ";
CHECK(sapi_height2.GetValue() == kHeight) << "Heights differ"; CHECK(sapi_height.GetValue() == kHeight) << "Heights differ";
// Transfer the pixels so they can be used here. // Transfer the pixels so they can be used here.
sapi::v::Array<uint8_t> sapi_pixels(kImgLen); sapi::v::Array<uint8_t> sapi_pixels(kImgLen);

View File

@ -65,7 +65,7 @@ TEST(LodePngTest, EncodeDecodeOneStep) {
ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox initialization"; ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox initialization";
LodepngApi api(&sandbox); LodepngApi api(&sandbox);
std::vector<uint8_t> image(GenerateValues()); std::vector<uint8_t> image = GenerateValues();
sapi::v::Array<uint8_t> sapi_image(kImgLen); sapi::v::Array<uint8_t> sapi_image(kImgLen);
EXPECT_THAT(std::copy(image.begin(), image.end(), sapi_image.GetData()), EXPECT_THAT(std::copy(image.begin(), image.end(), sapi_image.GetData()),
@ -81,18 +81,18 @@ TEST(LodePngTest, EncodeDecodeOneStep) {
ASSERT_THAT(result, Eq(0)) << "Unexpected result from encode32_file call"; ASSERT_THAT(result, Eq(0)) << "Unexpected result from encode32_file call";
sapi::v::UInt sapi_width2, sapi_height2; sapi::v::UInt sapi_width, sapi_height;
sapi::v::IntBase<uint8_t *> sapi_image_ptr(0); sapi::v::IntBase<uint8_t *> sapi_image_ptr(0);
SAPI_ASSERT_OK_AND_ASSIGN( SAPI_ASSERT_OK_AND_ASSIGN(
result, api.lodepng_decode32_file( result, api.lodepng_decode32_file(
sapi_image_ptr.PtrBoth(), sapi_width2.PtrBoth(), sapi_image_ptr.PtrBoth(), sapi_width.PtrBoth(),
sapi_height2.PtrBoth(), sapi_filename.PtrBefore())); sapi_height.PtrBoth(), sapi_filename.PtrBefore()));
ASSERT_THAT(result, Eq(0)) << "Unexpected result from decode32_file call"; ASSERT_THAT(result, Eq(0)) << "Unexpected result from decode32_file call";
EXPECT_THAT(sapi_width2.GetValue(), Eq(kWidth)) << "Widths differ"; EXPECT_THAT(sapi_width.GetValue(), Eq(kWidth)) << "Widths differ";
EXPECT_THAT(sapi_height2.GetValue(), Eq(kHeight)) << "Heights differ"; EXPECT_THAT(sapi_height.GetValue(), Eq(kHeight)) << "Heights differ";
sapi::v::Array<uint8_t> sapi_pixels(kImgLen); sapi::v::Array<uint8_t> sapi_pixels(kImgLen);
sapi_pixels.SetRemote(sapi_image_ptr.GetValue()); sapi_pixels.SetRemote(sapi_image_ptr.GetValue());
@ -126,7 +126,7 @@ TEST(LodePngTest, EncodeDecodeTwoSteps) {
ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox init"; ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox init";
LodepngApi api(&sandbox); LodepngApi api(&sandbox);
std::vector<uint8_t> image(GenerateValues()); std::vector<uint8_t> image = GenerateValues();
sapi::v::Array<uint8_t> sapi_image(kImgLen); sapi::v::Array<uint8_t> sapi_image(kImgLen);
EXPECT_THAT(std::copy(image.begin(), image.end(), sapi_image.GetData()), EXPECT_THAT(std::copy(image.begin(), image.end(), sapi_image.GetData()),
@ -158,7 +158,7 @@ TEST(LodePngTest, EncodeDecodeTwoSteps) {
ASSERT_THAT(result, Eq(0)) << "Unexpected result from save_file call"; ASSERT_THAT(result, Eq(0)) << "Unexpected result from save_file call";
sapi::v::UInt sapi_width2, sapi_height2; sapi::v::UInt sapi_width, sapi_height;
sapi::v::IntBase<uint8_t *> sapi_png_ptr2(0); sapi::v::IntBase<uint8_t *> sapi_png_ptr2(0);
sapi::v::ULLong sapi_pngsize2; sapi::v::ULLong sapi_pngsize2;
@ -181,14 +181,14 @@ TEST(LodePngTest, EncodeDecodeTwoSteps) {
sapi::v::IntBase<uint8_t *> sapi_png_ptr3(0); sapi::v::IntBase<uint8_t *> sapi_png_ptr3(0);
SAPI_ASSERT_OK_AND_ASSIGN( SAPI_ASSERT_OK_AND_ASSIGN(
result, result,
api.lodepng_decode32(sapi_png_ptr3.PtrBoth(), sapi_width2.PtrBoth(), api.lodepng_decode32(sapi_png_ptr3.PtrBoth(), sapi_width.PtrBoth(),
sapi_height2.PtrBoth(), sapi_png_array2.PtrBefore(), sapi_height.PtrBoth(), sapi_png_array2.PtrBefore(),
sapi_pngsize2.GetValue())); sapi_pngsize2.GetValue()));
ASSERT_THAT(result, Eq(0)) << "Unexpected result from decode32 call"; ASSERT_THAT(result, Eq(0)) << "Unexpected result from decode32 call";
EXPECT_THAT(sapi_width2.GetValue(), Eq(kWidth)) << "Widths differ"; EXPECT_THAT(sapi_width.GetValue(), Eq(kWidth)) << "Widths differ";
EXPECT_THAT(sapi_height2.GetValue(), Eq(kHeight)) << "Heights differ"; EXPECT_THAT(sapi_height.GetValue(), Eq(kHeight)) << "Heights differ";
sapi::v::Array<uint8_t> sapi_pixels(kImgLen); sapi::v::Array<uint8_t> sapi_pixels(kImgLen);
sapi_pixels.SetRemote(sapi_png_ptr3.GetValue()); sapi_pixels.SetRemote(sapi_png_ptr3.GetValue());

View File

@ -22,7 +22,7 @@
void EncodeDecodeOneStep(const std::string &images_path) { void EncodeDecodeOneStep(const std::string &images_path) {
// Generate the values. // Generate the values.
std::vector<uint8_t> image(GenerateValues()); std::vector<uint8_t> image = GenerateValues();
// Encode the image. // Encode the image.
const std::string filename = images_path + "/out_generated1.png"; const std::string filename = images_path + "/out_generated1.png";
@ -33,15 +33,15 @@ void EncodeDecodeOneStep(const std::string &images_path) {
// After the image has been encoded, decode it to check that the // After the image has been encoded, decode it to check that the
// pixel values are the same. // pixel values are the same.
unsigned int width2, height2; unsigned int width, height;
uint8_t *image2 = 0; uint8_t *image2 = 0;
result = lodepng_decode32_file(&image2, &width2, &height2, filename.c_str()); result = lodepng_decode32_file(&image2, &width, &height, filename.c_str());
CHECK(!result) << "Unexpected result from decode32_file call"; CHECK(!result) << "Unexpected result from decode32_file call";
CHECK(width2 == kWidth) << "Widths differ"; CHECK(width == kWidth) << "Widths differ";
CHECK(height2 == kHeight) << "Heights differ"; CHECK(height == kHeight) << "Heights differ";
// Now, we can compare the values. // Now, we can compare the values.
CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen)) CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen))
@ -52,7 +52,7 @@ void EncodeDecodeOneStep(const std::string &images_path) {
void EncodeDecodeTwoSteps(const std::string &images_path) { void EncodeDecodeTwoSteps(const std::string &images_path) {
// Generate the values. // Generate the values.
std::vector<uint8_t> image(GenerateValues()); std::vector<uint8_t> image = GenerateValues();
// Encode the image into memory first. // Encode the image into memory first.
const std::string filename = images_path + "/out_generated2.png"; const std::string filename = images_path + "/out_generated2.png";
@ -70,7 +70,7 @@ void EncodeDecodeTwoSteps(const std::string &images_path) {
CHECK(!result) << "Unexpected result from save_file call"; CHECK(!result) << "Unexpected result from save_file call";
// Now, decode the image using the 2 steps in order to compare the values. // Now, decode the image using the 2 steps in order to compare the values.
unsigned int width2, height2; unsigned int width, height;
uint8_t *png2; uint8_t *png2;
size_t pngsize2; size_t pngsize2;
@ -81,11 +81,11 @@ void EncodeDecodeTwoSteps(const std::string &images_path) {
CHECK(pngsize == pngsize2) << "Png sizes differ"; CHECK(pngsize == pngsize2) << "Png sizes differ";
uint8_t *image2; uint8_t *image2;
result = lodepng_decode32(&image2, &width2, &height2, png2, pngsize2); result = lodepng_decode32(&image2, &width, &height, png2, pngsize2);
CHECK(!result) << "Unexpected result from decode32 call"; CHECK(!result) << "Unexpected result from decode32 call";
CHECK(width2 == kWidth) << "Widths differ"; CHECK(width == kWidth) << "Widths differ";
CHECK(height2 == kHeight) << "Heights differ"; CHECK(height == kHeight) << "Heights differ";
// Compare the values. // Compare the values.
CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen)) CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen))