• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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 <vector>
6 
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "third_party/base/span.h"
9 
10 // Tests PDFium-modifications to base::span. The name of this file is
11 // chosen to avoid collisions with base's span_unittest.cc
12 
TEST(PdfiumSpan,EmptySpan)13 TEST(PdfiumSpan, EmptySpan) {
14   int stuff[] = {1, 2, 3};
15   pdfium::span<int> null_span;
16   pdfium::span<int> stuff_span(stuff);
17   pdfium::span<int> empty_first_span = stuff_span.first(0);
18   pdfium::span<int> empty_last_span = stuff_span.last(0);
19   pdfium::span<int> empty_sub_span1 = stuff_span.subspan(0, 0);
20   pdfium::span<int> empty_sub_span2 = stuff_span.subspan(3, 0);
21   EXPECT_TRUE(null_span.empty());
22   EXPECT_TRUE(empty_first_span.empty());
23   EXPECT_TRUE(empty_last_span.empty());
24   EXPECT_TRUE(empty_sub_span1.empty());
25   EXPECT_TRUE(empty_sub_span2.empty());
26 }
27 
28 // Custom implementation of first()/last().
TEST(PdfiumSpan,FirstLast)29 TEST(PdfiumSpan, FirstLast) {
30   int one[] = {1};
31   int stuff[] = {1, 2, 3};
32   pdfium::span<int> one_span(one);
33   pdfium::span<int> stuff_span(stuff);
34   EXPECT_EQ(one_span.front(), 1);
35   EXPECT_EQ(one_span.back(), 1);
36   EXPECT_EQ(stuff_span.front(), 1);
37   EXPECT_EQ(stuff_span.back(), 3);
38 }
39 
TEST(PdfiumSpanDeathTest,EmptySpanIndex)40 TEST(PdfiumSpanDeathTest, EmptySpanIndex) {
41   pdfium::span<int> empty_span;
42   EXPECT_DEATH(empty_span[0] += 1, ".*");
43 }
44 
TEST(PdfiumSpanDeathTest,EmptySpanFront)45 TEST(PdfiumSpanDeathTest, EmptySpanFront) {
46   pdfium::span<int> empty_span;
47   EXPECT_DEATH(empty_span.front() += 1, ".*");
48 }
49 
TEST(PdfiumSpanDeathTest,EmptySpanBack)50 TEST(PdfiumSpanDeathTest, EmptySpanBack) {
51   pdfium::span<int> empty_span;
52   EXPECT_DEATH(empty_span.back() += 1, ".*");
53 }
54 
55 #if defined(ADDRESS_SANITIZER)
56 namespace {
57 
CreateDanglingSpan()58 void CreateDanglingSpan() {
59   pdfium::span<int> data_span;
60   {
61     std::vector<int> data(4);
62     data_span = pdfium::make_span(data);
63   }
64 }
65 
66 }  // namespace
67 
TEST(PdfiumSpanDeathTest,DanglingReference)68 TEST(PdfiumSpanDeathTest, DanglingReference) {
69   EXPECT_DEATH(CreateDanglingSpan(), ".*");
70 }
71 #endif  // defined(ADDRESS_SANITIZER)
72