Add test showing copies do not happen in fxcrt::Zip().
Show that the result is an iteration by reference rather than by
creating temporary values.
Change-Id: Ia1d3f12a9e2b8eddad7a37a001b99005dffcfbad
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/127791
Reviewed-by: Lei Zhang <thestig@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcrt/zip_unittest.cpp b/core/fxcrt/zip_unittest.cpp
index 211fd8d..40666c2 100644
--- a/core/fxcrt/zip_unittest.cpp
+++ b/core/fxcrt/zip_unittest.cpp
@@ -4,6 +4,7 @@
#include <stdint.h>
+#include "core/fxcrt/notreached.h"
#include "core/fxcrt/span.h"
#include "core/fxcrt/zip.h"
#include "testing/gmock/include/gmock/gmock.h"
@@ -12,6 +13,25 @@
using ::testing::ElementsAreArray;
namespace fxcrt {
+namespace {
+
+struct NoCopyNoMove {
+ NoCopyNoMove(int val) : value(val) {}
+ NoCopyNoMove(const NoCopyNoMove&) { NOTREACHED(); }
+ NoCopyNoMove(NoCopyNoMove&&) { NOTREACHED(); }
+ NoCopyNoMove& operator=(const NoCopyNoMove&) {
+ NOTREACHED();
+ return *this;
+ }
+ NoCopyNoMove& operator=(NoCopyNoMove&&) {
+ NOTREACHED();
+ return *this;
+ }
+
+ int value;
+};
+
+} // namespace
TEST(Zip, EmptyZip) {
pdfium::span<const int> nothing;
@@ -87,6 +107,25 @@
}
}
+TEST(Zip, NoCopy) {
+ // Test that copies do not silently happen when zipping.
+ const NoCopyNoMove stuff1[] = {{1}, {2}, {3}};
+ const NoCopyNoMove expected[] = {{1}, {2}, {3}};
+ for (auto [in1, exp] : Zip(stuff1, expected)) {
+ EXPECT_EQ(exp.value, in1.value);
+ }
+}
+
+TEST(Zip, NoCopy3) {
+ // Test that copies do not silently happen when zipping.
+ const NoCopyNoMove stuff1[] = {{1}, {2}, {3}};
+ const NoCopyNoMove stuff2[] = {{4}, {5}, {6}};
+ const NoCopyNoMove expected[] = {{5}, {7}, {9}};
+ for (auto [in1, in2, exp] : Zip(stuff1, stuff2, expected)) {
+ EXPECT_EQ(exp.value, in1.value + in2.value);
+ }
+}
+
TEST(Zip, BadArgumentsZip) {
pdfium::span<const int> nothing;
int stuff[] = {1, 2, 3};