dawn-cmake/src/tint/writer/spirv/operand_test.cc
Ben Clayton 7e8df044c6 tint/writer/spirv: Replace Operand with std::variant
Operand is just a tagged union of uint32_t, float and std::string.

Use std::variant for this.
Reduces memory size, and removes the need to always construct an empty string when the operand is float or int.

Bug: tint:1383
Change-Id: I02fc10137d6fab410ea25a8d6c6e279b882b2287
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/88302
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
2022-04-28 18:40:03 +00:00

64 lines
1.7 KiB
C++

// Copyright 2020 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/tint/writer/spirv/operand.h"
#include "gtest/gtest.h"
namespace tint::writer::spirv {
namespace {
using OperandTest = testing::Test;
TEST_F(OperandTest, CreateFloat) {
auto o = Operand(1.2f);
ASSERT_TRUE(std::holds_alternative<float>(o));
EXPECT_FLOAT_EQ(std::get<float>(o), 1.2f);
}
TEST_F(OperandTest, CreateInt) {
auto o = Operand(1u);
ASSERT_TRUE(std::holds_alternative<uint32_t>(o));
EXPECT_EQ(std::get<uint32_t>(o), 1u);
}
TEST_F(OperandTest, CreateString) {
auto o = Operand("my string");
ASSERT_TRUE(std::holds_alternative<std::string>(o));
EXPECT_EQ(std::get<std::string>(o), "my string");
}
TEST_F(OperandTest, Length_Float) {
auto o = Operand(1.2f);
EXPECT_EQ(OperandLength(o), 1u);
}
TEST_F(OperandTest, Length_Int) {
auto o = U32Operand(1);
EXPECT_EQ(OperandLength(o), 1u);
}
TEST_F(OperandTest, Length_String) {
auto o = Operand("my string");
EXPECT_EQ(OperandLength(o), 3u);
}
TEST_F(OperandTest, Length_String_Empty) {
auto o = Operand("");
EXPECT_EQ(OperandLength(o), 1u);
}
} // namespace
} // namespace tint::writer::spirv