From 3d015be05c1dcb17e3491e5639fa5a9132276a02 Mon Sep 17 00:00:00 2001 From: Andrei Medar Date: Wed, 12 Aug 2020 11:20:02 +0000 Subject: [PATCH] added some comments in main_unit_test.cc and .gitignore --- oss-internship-2020/sapi_lodepng/.gitignore | 1 + .../sapi_lodepng/main_unit_test.cc | 37 ++++++++++++------- 2 files changed, 25 insertions(+), 13 deletions(-) create mode 100644 oss-internship-2020/sapi_lodepng/.gitignore diff --git a/oss-internship-2020/sapi_lodepng/.gitignore b/oss-internship-2020/sapi_lodepng/.gitignore new file mode 100644 index 0000000..d163863 --- /dev/null +++ b/oss-internship-2020/sapi_lodepng/.gitignore @@ -0,0 +1 @@ +build/ \ No newline at end of file diff --git a/oss-internship-2020/sapi_lodepng/main_unit_test.cc b/oss-internship-2020/sapi_lodepng/main_unit_test.cc index 019f8a0..b0f9555 100644 --- a/oss-internship-2020/sapi_lodepng/main_unit_test.cc +++ b/oss-internship-2020/sapi_lodepng/main_unit_test.cc @@ -47,7 +47,8 @@ TEST(generate_image, encode_decode_compare_one_step) { SapiLodepngSandbox sandbox(images_path); ASSERT_TRUE(sandbox.Init().ok()); LodepngApi api(&sandbox); - // std::cout << "path = " << images_path << std::endl; + + // generate the values unsigned int width = 512, height = 512; unsigned char *image = (unsigned char *)malloc(width * height * 4); @@ -60,14 +61,12 @@ TEST(generate_image, encode_decode_compare_one_step) { } } + // encode the image sapi::v::Array sapi_image(image, width * height * 4); sapi::v::UInt sapi_width(width), sapi_height(height); std::string filename = images_path + "/out_generated1.png"; sapi::v::ConstCStr sapi_filename(filename.c_str()); - // ASSERT_TRUE(sandbox.Allocate(&image_).ok()); - // ASSERT_TRUE(sandbox.TransferToSandboxee(&image_).ok()); - sapi::StatusOr result = api.lodepng_encode32_file( sapi_filename.PtrBefore(), sapi_image.PtrBefore(), sapi_width.GetValue(), sapi_height.GetValue()); @@ -75,6 +74,9 @@ TEST(generate_image, encode_decode_compare_one_step) { ASSERT_TRUE(result.ok()); ASSERT_EQ(result.value(), 0); + // after the image has been encoded, decode it to check that the + // pixel values are the same + sapi::v::UInt sapi_width2, sapi_height2; sapi::v::IntBase sapi_image_ptr(0); @@ -88,6 +90,10 @@ TEST(generate_image, encode_decode_compare_one_step) { ASSERT_EQ(sapi_width2.GetValue(), width); ASSERT_EQ(sapi_height2.GetValue(), height); + // the pixels have been allocated inside the sandboxed process + // memory, so we need to transfer them to this process. + // Transferring the memory has the following steps: + // 1) define a RemotePtr variable sapi::v::RemotePtr sapi_remote_out_ptr( reinterpret_cast(sapi_image_ptr.GetValue())); sapi::v::Array sapi_pixels(sapi_width2.GetValue() * @@ -96,8 +102,11 @@ TEST(generate_image, encode_decode_compare_one_step) { ASSERT_TRUE(sandbox.TransferFromSandboxee(&sapi_pixels).ok()); + // after the memory has been transferred, we can access it + // using the GetData function unsigned char *pixels_ptr = sapi_pixels.GetData(); + // now, we can compare the values for (size_t i = 0; i < width * height * 4; ++i) { ASSERT_EQ(pixels_ptr[i], image[i]); } @@ -107,13 +116,13 @@ TEST(generate_image, encode_decode_compare_one_step) { // similar to the previous test, only that we use encoding by saving the data in // memory and then writing it to the file and decoding by first decoding in -// memory and then getting the pixels. +// memory and then getting the actual pixel values. TEST(generate_image, encode_decode_compare_two_step) { SapiLodepngSandbox sandbox(images_path); ASSERT_TRUE(sandbox.Init().ok()); LodepngApi api(&sandbox); - // generate the image + // generate the values unsigned int width = 512, height = 512; unsigned char *image = (unsigned char *)malloc(width * height * 4); @@ -126,6 +135,7 @@ TEST(generate_image, encode_decode_compare_two_step) { } } + // encode the image into memory first sapi::v::Array sapi_image(image, width * height * 4); sapi::v::UInt sapi_width(width), sapi_height(height); std::string filename = images_path + "/out_generated2.png"; @@ -145,7 +155,9 @@ TEST(generate_image, encode_decode_compare_two_step) { std::cout << "sapi_pngsize = " << sapi_pngsize.GetValue() << std::endl; - // transfer the array from the sandboxed process + // the new array (pointed to by sapi_png_ptr) is allocated + // inside the sandboxed process so we need to transfer it to this + // process sapi::v::RemotePtr sapi_remote_out_ptr( reinterpret_cast(sapi_png_ptr.GetValue())); @@ -163,12 +175,13 @@ TEST(generate_image, encode_decode_compare_two_step) { ASSERT_TRUE(result.ok()); ASSERT_EQ(result.value(), 0); - // now, decode the image using the 2 steps + // now, decode the image using the 2 steps in order to compare the values sapi::v::UInt sapi_width2, sapi_height2; sapi::v::IntBase sapi_png_ptr2(0); sapi::v::ULLong sapi_pngsize2; + // load the file in memory result = api.lodepng_load_file(sapi_png_ptr2.PtrBoth(), sapi_pngsize2.PtrBoth(), sapi_filename.PtrBefore()); @@ -178,6 +191,7 @@ TEST(generate_image, encode_decode_compare_two_step) { ASSERT_EQ(sapi_pngsize.GetValue(), sapi_pngsize2.GetValue()); + // transfer the png array sapi::v::RemotePtr sapi_remote_out_ptr2( reinterpret_cast(sapi_png_ptr2.GetValue())); sapi::v::Array sapi_png_array2(sapi_pngsize2.GetValue()); @@ -186,9 +200,9 @@ TEST(generate_image, encode_decode_compare_two_step) { ASSERT_TRUE(sandbox.TransferFromSandboxee(&sapi_png_array2).ok()); - // after the file is loaded, decode it + // after the file is loaded, decode it so we have access to the values + // directly sapi::v::IntBase sapi_png_ptr3(0); - // sapi::v::UInt sapi_width2, sapi_height2; result = api.lodepng_decode32( sapi_png_ptr3.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(), sapi_png_array2.PtrBefore(), sapi_pngsize2.GetValue()); @@ -196,9 +210,6 @@ TEST(generate_image, encode_decode_compare_two_step) { ASSERT_TRUE(result.ok()); ASSERT_EQ(result.value(), 0); - std::cout << "w2 = " << sapi_width2.GetValue() - << " h2 = " << sapi_height2.GetValue() << std::endl; - ASSERT_EQ(sapi_width2.GetValue(), width); ASSERT_EQ(sapi_height2.GetValue(), height);