• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "core/fxcrt/span_util.h"
6 
7 #include <vector>
8 
9 #include "testing/gtest/include/gtest/gtest.h"
10 
TEST(Spanset,Fits)11 TEST(Spanset, Fits) {
12   std::vector<char> dst(4, 'B');
13   fxcrt::spanset(pdfium::make_span(dst).first(2), 'A');
14   EXPECT_EQ(dst[0], 'A');
15   EXPECT_EQ(dst[1], 'A');
16   EXPECT_EQ(dst[2], 'B');
17   EXPECT_EQ(dst[3], 'B');
18 }
19 
TEST(Spanset,Empty)20 TEST(Spanset, Empty) {
21   std::vector<char> dst(4, 'B');
22   fxcrt::spanset(pdfium::make_span(dst).subspan(4), 'A');
23   EXPECT_EQ(dst[0], 'B');
24   EXPECT_EQ(dst[1], 'B');
25   EXPECT_EQ(dst[2], 'B');
26   EXPECT_EQ(dst[3], 'B');
27 }
28 
TEST(Spancpy,FitsEntirely)29 TEST(Spancpy, FitsEntirely) {
30   std::vector<char> src(4, 'A');
31   std::vector<char> dst(4, 'B');
32   fxcrt::spancpy(pdfium::make_span(dst), pdfium::make_span(src));
33   EXPECT_EQ(dst[0], 'A');
34   EXPECT_EQ(dst[1], 'A');
35   EXPECT_EQ(dst[2], 'A');
36   EXPECT_EQ(dst[3], 'A');
37 }
38 
TEST(Spancpy,FitsWithin)39 TEST(Spancpy, FitsWithin) {
40   std::vector<char> src(2, 'A');
41   std::vector<char> dst(4, 'B');
42   // Also show that a const src argument is acceptable.
43   fxcrt::spancpy(pdfium::make_span(dst).subspan(1),
44                  pdfium::span<const char>(src));
45   EXPECT_EQ(dst[0], 'B');
46   EXPECT_EQ(dst[1], 'A');
47   EXPECT_EQ(dst[2], 'A');
48   EXPECT_EQ(dst[3], 'B');
49 }
50 
TEST(Spancpy,EmptyCopyWithin)51 TEST(Spancpy, EmptyCopyWithin) {
52   std::vector<char> src(2, 'A');
53   std::vector<char> dst(4, 'B');
54   fxcrt::spancpy(pdfium::make_span(dst).subspan(1),
55                  pdfium::make_span(src).subspan(2));
56   EXPECT_EQ(dst[0], 'B');
57   EXPECT_EQ(dst[1], 'B');
58   EXPECT_EQ(dst[2], 'B');
59   EXPECT_EQ(dst[3], 'B');
60 }
61 
TEST(Spancpy,EmptyCopyToEmpty)62 TEST(Spancpy, EmptyCopyToEmpty) {
63   std::vector<char> src(2, 'A');
64   std::vector<char> dst(4, 'B');
65   fxcrt::spancpy(pdfium::make_span(dst).subspan(4),
66                  pdfium::make_span(src).subspan(2));
67   EXPECT_EQ(dst[0], 'B');
68   EXPECT_EQ(dst[1], 'B');
69   EXPECT_EQ(dst[2], 'B');
70   EXPECT_EQ(dst[3], 'B');
71 }
72 
TEST(Spanmove,FitsWithin)73 TEST(Spanmove, FitsWithin) {
74   std::vector<char> src(2, 'A');
75   std::vector<char> dst(4, 'B');
76   // Also show that a const src argument is acceptable.
77   fxcrt::spanmove(pdfium::make_span(dst).subspan(1),
78                   pdfium::span<const char>(src));
79   EXPECT_EQ(dst[0], 'B');
80   EXPECT_EQ(dst[1], 'A');
81   EXPECT_EQ(dst[2], 'A');
82   EXPECT_EQ(dst[3], 'B');
83 }
84 
TEST(Span,AssignOverOnePastEnd)85 TEST(Span, AssignOverOnePastEnd) {
86   std::vector<char> src(2, 'A');
87   pdfium::span<char> span = pdfium::make_span(src);
88   span = span.subspan(2);
89   span = pdfium::make_span(src);
90   EXPECT_EQ(span.size(), 2u);
91 }
92