/* * Copyright 2022 Google LLC * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include "include/core/SkSpan.h" #include "tests/Test.h" #include #include DEF_TEST(SkSpanBasicTemplateGuide, reporter) { // Test constness preservation for SkSpan. { std::vector v = {{1, 2, 3, 4, 5}}; auto s = SkSpan(v); REPORTER_ASSERT(reporter, s[3] == 4); s[3] = 100; REPORTER_ASSERT(reporter, s[3] == 100); } { std::vector t = {{1, 2, 3, 4, 5}}; const std::vector& v = t; auto s = SkSpan(v); //s[3] = 100; // Should fail to compile REPORTER_ASSERT(reporter, s[3] == 4); REPORTER_ASSERT(reporter, t[3] == 4); t[3] = 100; REPORTER_ASSERT(reporter, s[3] == 100); } { std::array v = {{1, 2, 3, 4, 5}}; auto s = SkSpan(v); // {1, 2, 3, 4, 5} REPORTER_ASSERT(reporter, s[3] == 4); s[3] = 100; // {1, 2, 3, 100, 5} REPORTER_ASSERT(reporter, s[3] == 100); auto s1 = s.subspan(1,3); // {2, 3, 100} REPORTER_ASSERT(reporter, s1.size() == 3); REPORTER_ASSERT(reporter, s1.front() == 2); REPORTER_ASSERT(reporter, s1.back() == 100); auto s2 = s.subspan(2); // {3, 100, 5} REPORTER_ASSERT(reporter, s2.size() == 3); REPORTER_ASSERT(reporter, s2.front() == 3); REPORTER_ASSERT(reporter, s2.back() == 5); } { std::array t = {{1, 2, 3, 4, 5}}; const std::array& v = t; auto s = SkSpan(v); //s[3] = 100; // Should fail to compile REPORTER_ASSERT(reporter, s[3] == 4); REPORTER_ASSERT(reporter, t[3] == 4); t[3] = 100; REPORTER_ASSERT(reporter, s[3] == 100); } { std::vector v; auto s = SkSpan(v); REPORTER_ASSERT(reporter, s.empty()); } auto routine = [&](SkSpan a) { REPORTER_ASSERT(reporter, a.size() == 4); }; routine({1,2,3,4}); } static bool test_span_parameter(SkSpan s) { return s[0] == 1 && s[1] == 2 && s[2] == 3; } DEF_TEST(SkSpanDeduceParam, reporter) { { std::vector v = {{1, 2, 3}}; REPORTER_ASSERT(reporter, test_span_parameter(v)); } { int v[]{1, 2, 3}; REPORTER_ASSERT(reporter, test_span_parameter(v)); } { test_span_parameter({1, 2, 3}); REPORTER_ASSERT(reporter, test_span_parameter({1, 2, 3})); } { int v[]{1, 2, 3}; auto s = SkSpan(v); REPORTER_ASSERT(reporter, test_span_parameter(s)); } }