Update documentation and tests for fxcrt::Zip() It turns out that zip(), as currently written, can be used to compare two const spans, because `T&&` inside a template is interpreted as a forwarding reference. Document and test this before using it in code in this manner. Change-Id: I8ad7b335cb5ca799a8afc269e000e29a6f172cc5 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/127770 Commit-Queue: Tom Sepez <tsepez@chromium.org> Reviewed-by: Lei Zhang <thestig@chromium.org> Reviewed-by: Thomas Sepez <tsepez@google.com>
diff --git a/core/fxcrt/zip.h b/core/fxcrt/zip.h index 4884a0d..8902f19 100644 --- a/core/fxcrt/zip.h +++ b/core/fxcrt/zip.h
@@ -36,10 +36,12 @@ // - Only zips together two or three views instead of N. // - Size is determined by the first view, which must be smaller than the // other view(s). -// - With two views, the first view is presumed to be "input-like" and is const, -// second view is presumed to be "output-like" and is non-const. +// - With two views, the first view is presumed to be "input-like" and is const. +// The second view is presumed to be "output-like", can be non-const, and +// can be assigned-to if desired. // - With three views, the first two views are presumed to be "input-like" and -// are const. +// are const. The third view is is presumed to be "output-like", can be +// non-const, and can be assigned-to if desired. // - Only those methods required to support use in a range-based for-loop // are provided.
diff --git a/core/fxcrt/zip_unittest.cpp b/core/fxcrt/zip_unittest.cpp index 46add43..09a2891 100644 --- a/core/fxcrt/zip_unittest.cpp +++ b/core/fxcrt/zip_unittest.cpp
@@ -43,6 +43,11 @@ out = in; } EXPECT_THAT(output, ElementsAreArray(expected)); + + // The "output" span can, in fact, be const so long as we don't assign to it. + for (auto [in, expect] : Zip(stuff, expected)) { + EXPECT_EQ(in, expect); + } } TEST(Zip, ActualZip3) { @@ -55,6 +60,11 @@ out = in1 + in2; } EXPECT_THAT(output, ElementsAreArray(expected)); + + // The "output" span can, in fact, be const so long as we don't assign to it. + for (auto [in1, in2, expect] : Zip(stuff1, stuff2, output)) { + EXPECT_EQ(in1 + in2, expect); + } } TEST(Zip, BadArgumentsZip) {