Path: blob/main/libshaderc_util/src/counting_includer_test.cc
1560 views
// Copyright 2015 The Shaderc Authors. All rights reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314#include "libshaderc_util/counting_includer.h"1516#include <thread>17#include <vector>1819#include <gmock/gmock.h>2021namespace {2223// A trivial implementation of CountingIncluder's virtual methods, so tests can24// instantiate.25class ConcreteCountingIncluder : public shaderc_util::CountingIncluder {26public:27using IncludeResult = glslang::TShader::Includer::IncludeResult;28~ConcreteCountingIncluder() {29// Avoid leaks.30for (auto result : results_) {31release_delegate(result);32}33}34virtual IncludeResult* include_delegate(35const char* requested, const char* requestor, IncludeType,36size_t) override {37const char kError[] = "Unexpected #include";38results_.push_back(new IncludeResult{"", kError, strlen(kError), nullptr});39return results_.back();40}41virtual void release_delegate(IncludeResult* include_result) override {42delete include_result;43}4445private:46// All the results we've returned so far.47std::vector<IncludeResult*> results_;48};4950TEST(CountingIncluderTest, InitialCount) {51EXPECT_EQ(0, ConcreteCountingIncluder().num_include_directives());52}5354TEST(CountingIncluderTest, OneIncludeLocal) {55ConcreteCountingIncluder includer;56includer.includeLocal("random file name", "from me", 0);57EXPECT_EQ(1, includer.num_include_directives());58}5960TEST(CountingIncluderTest, TwoIncludesAnyIncludeType) {61ConcreteCountingIncluder includer;62includer.includeSystem("name1", "from me", 0);63includer.includeLocal("name2", "me", 0);64EXPECT_EQ(2, includer.num_include_directives());65}6667TEST(CountingIncluderTest, ManyIncludes) {68ConcreteCountingIncluder includer;69for (int i = 0; i < 100; ++i) {70includer.includeLocal("filename", "from me", i);71includer.includeSystem("filename", "from me", i);72}73EXPECT_EQ(200, includer.num_include_directives());74}7576#ifndef SHADERC_DISABLE_THREADED_TESTS77TEST(CountingIncluderTest, ThreadedIncludes) {78ConcreteCountingIncluder includer;79std::thread t1(80[&includer]() { includer.includeLocal("name1", "me", 0); });81std::thread t2(82[&includer]() { includer.includeSystem("name2", "me", 1); });83std::thread t3(84[&includer]() { includer.includeLocal("name3", "me", 2); });85t1.join();86t2.join();87t3.join();88EXPECT_EQ(3, includer.num_include_directives());89}90#endif // SHADERC_DISABLE_THREADED_TESTS9192} // anonymous namespace939495