2015-05-22 17:26:19 +00:00
|
|
|
// Copyright (c) 2015 The Khronos Group Inc.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and/or associated documentation files (the
|
|
|
|
// "Materials"), to deal in the Materials without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Materials, and to
|
|
|
|
// permit persons to whom the Materials are furnished to do so, subject to
|
|
|
|
// the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Materials.
|
|
|
|
//
|
|
|
|
// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
|
|
|
|
// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
|
|
|
|
// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
|
|
|
|
// https://www.khronos.org/registry/
|
|
|
|
//
|
|
|
|
// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
|
|
|
|
|
2015-11-06 23:08:49 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-11-02 16:40:59 +00:00
|
|
|
#include "gmock/gmock.h"
|
2015-11-11 19:24:04 +00:00
|
|
|
|
|
|
|
#include "TestFixture.h"
|
2015-05-22 17:26:19 +00:00
|
|
|
#include "UnitSPIRV.h"
|
2015-10-23 13:53:58 +00:00
|
|
|
#include "util/bitutils.h"
|
2015-11-11 19:24:04 +00:00
|
|
|
#include "../source/spirv_constant.h"
|
2015-10-23 13:53:58 +00:00
|
|
|
|
2015-08-31 19:24:32 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-09-24 14:26:51 +00:00
|
|
|
using libspirv::AssemblyContext;
|
|
|
|
using libspirv::AssemblyGrammar;
|
2015-10-01 20:58:17 +00:00
|
|
|
using spvtest::AutoText;
|
2015-11-06 16:23:57 +00:00
|
|
|
using spvtest::Concatenate;
|
|
|
|
using spvtest::MakeInstruction;
|
|
|
|
using spvtest::TextToBinaryTest;
|
2015-11-02 16:40:59 +00:00
|
|
|
using testing::Eq;
|
2015-08-31 19:24:32 +00:00
|
|
|
|
2015-09-11 16:04:03 +00:00
|
|
|
TEST(GetWord, Simple) {
|
2015-09-24 14:26:51 +00:00
|
|
|
EXPECT_EQ("", AssemblyContext(AutoText(""), nullptr).getWord());
|
|
|
|
EXPECT_EQ("", AssemblyContext(AutoText("\0a"), nullptr).getWord());
|
|
|
|
EXPECT_EQ("", AssemblyContext(AutoText(" a"), nullptr).getWord());
|
|
|
|
EXPECT_EQ("", AssemblyContext(AutoText("\ta"), nullptr).getWord());
|
|
|
|
EXPECT_EQ("", AssemblyContext(AutoText("\va"), nullptr).getWord());
|
|
|
|
EXPECT_EQ("", AssemblyContext(AutoText("\ra"), nullptr).getWord());
|
|
|
|
EXPECT_EQ("", AssemblyContext(AutoText("\na"), nullptr).getWord());
|
|
|
|
EXPECT_EQ("abc", AssemblyContext(AutoText("abc"), nullptr).getWord());
|
|
|
|
EXPECT_EQ("abc", AssemblyContext(AutoText("abc "), nullptr).getWord());
|
2015-11-02 14:41:20 +00:00
|
|
|
EXPECT_EQ("abc", AssemblyContext(AutoText("abc\t"), nullptr).getWord());
|
|
|
|
EXPECT_EQ("abc", AssemblyContext(AutoText("abc\r"), nullptr).getWord());
|
|
|
|
EXPECT_EQ("abc", AssemblyContext(AutoText("abc\v"), nullptr).getWord());
|
|
|
|
EXPECT_EQ("abc", AssemblyContext(AutoText("abc\n"), nullptr).getWord());
|
2015-09-11 16:04:03 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 22:32:54 +00:00
|
|
|
// An mask parsing test case.
|
|
|
|
struct MaskCase {
|
2015-09-18 15:50:54 +00:00
|
|
|
spv_operand_type_t which_enum;
|
|
|
|
uint32_t expected_value;
|
2015-09-18 16:08:36 +00:00
|
|
|
const char* expression;
|
2015-09-16 22:32:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using GoodMaskParseTest = ::testing::TestWithParam<MaskCase>;
|
|
|
|
|
|
|
|
TEST_P(GoodMaskParseTest, GoodMaskExpressions) {
|
2015-11-12 18:48:30 +00:00
|
|
|
spv_context context = spvContextCreate();
|
2015-09-16 22:32:54 +00:00
|
|
|
|
|
|
|
uint32_t value;
|
2015-11-12 18:48:30 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS,
|
|
|
|
AssemblyGrammar(context).parseMaskOperand(
|
|
|
|
GetParam().which_enum, GetParam().expression, &value));
|
2015-09-16 22:32:54 +00:00
|
|
|
EXPECT_EQ(GetParam().expected_value, value);
|
2015-11-12 18:48:30 +00:00
|
|
|
|
|
|
|
spvContextDestroy(context);
|
2015-09-16 22:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(
|
|
|
|
ParseMask, GoodMaskParseTest,
|
|
|
|
::testing::ValuesIn(std::vector<MaskCase>{
|
|
|
|
{SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 0, "None"},
|
|
|
|
{SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 1, "NotNaN"},
|
|
|
|
{SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 2, "NotInf"},
|
|
|
|
{SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 3, "NotNaN|NotInf"},
|
|
|
|
// Mask experssions are symmetric.
|
|
|
|
{SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 3, "NotInf|NotNaN"},
|
|
|
|
// Repeating a value has no effect.
|
|
|
|
{SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 3, "NotInf|NotNaN|NotInf"},
|
|
|
|
// Using 3 operands still works.
|
|
|
|
{SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 0x13, "NotInf|NotNaN|Fast"},
|
|
|
|
{SPV_OPERAND_TYPE_SELECTION_CONTROL, 0, "None"},
|
|
|
|
{SPV_OPERAND_TYPE_SELECTION_CONTROL, 1, "Flatten"},
|
|
|
|
{SPV_OPERAND_TYPE_SELECTION_CONTROL, 2, "DontFlatten"},
|
|
|
|
// Weirdly, you can specify to flatten and don't flatten a selection.
|
|
|
|
{SPV_OPERAND_TYPE_SELECTION_CONTROL, 3, "Flatten|DontFlatten"},
|
|
|
|
{SPV_OPERAND_TYPE_LOOP_CONTROL, 0, "None"},
|
|
|
|
{SPV_OPERAND_TYPE_LOOP_CONTROL, 1, "Unroll"},
|
|
|
|
{SPV_OPERAND_TYPE_LOOP_CONTROL, 2, "DontUnroll"},
|
|
|
|
// Weirdly, you can specify to unroll and don't unroll a loop.
|
|
|
|
{SPV_OPERAND_TYPE_LOOP_CONTROL, 3, "Unroll|DontUnroll"},
|
|
|
|
{SPV_OPERAND_TYPE_FUNCTION_CONTROL, 0, "None"},
|
|
|
|
{SPV_OPERAND_TYPE_FUNCTION_CONTROL, 1, "Inline"},
|
|
|
|
{SPV_OPERAND_TYPE_FUNCTION_CONTROL, 2, "DontInline"},
|
|
|
|
{SPV_OPERAND_TYPE_FUNCTION_CONTROL, 4, "Pure"},
|
|
|
|
{SPV_OPERAND_TYPE_FUNCTION_CONTROL, 8, "Const"},
|
|
|
|
{SPV_OPERAND_TYPE_FUNCTION_CONTROL, 0xd, "Inline|Const|Pure"},
|
|
|
|
}));
|
|
|
|
|
|
|
|
using BadFPFastMathMaskParseTest = ::testing::TestWithParam<const char*>;
|
|
|
|
|
|
|
|
TEST_P(BadFPFastMathMaskParseTest, BadMaskExpressions) {
|
2015-11-12 18:48:30 +00:00
|
|
|
spv_context context = spvContextCreate();
|
2015-09-16 22:32:54 +00:00
|
|
|
|
|
|
|
uint32_t value;
|
2015-09-24 14:26:51 +00:00
|
|
|
EXPECT_NE(SPV_SUCCESS,
|
2015-11-12 18:48:30 +00:00
|
|
|
AssemblyGrammar(context).parseMaskOperand(
|
|
|
|
SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, GetParam(), &value));
|
|
|
|
|
|
|
|
spvContextDestroy(context);
|
2015-09-16 22:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(ParseMask, BadFPFastMathMaskParseTest,
|
|
|
|
::testing::ValuesIn(std::vector<const char*>{
|
|
|
|
nullptr, "", "NotValidEnum", "|", "NotInf|",
|
|
|
|
"|NotInf", "NotInf||NotNaN",
|
|
|
|
"Unroll" // A good word, but for the wrong enum
|
|
|
|
}));
|
|
|
|
|
2015-09-11 16:04:03 +00:00
|
|
|
// TODO(dneto): Aliasing like this relies on undefined behaviour. Fix this.
|
2015-05-22 17:26:19 +00:00
|
|
|
union char_word_t {
|
|
|
|
char cs[4];
|
|
|
|
uint32_t u;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, InvalidText) {
|
|
|
|
spv_binary binary;
|
|
|
|
ASSERT_EQ(SPV_ERROR_INVALID_TEXT,
|
2015-11-12 18:48:30 +00:00
|
|
|
spvTextToBinary(context, nullptr, 0, &binary, &diagnostic));
|
2015-11-02 16:40:59 +00:00
|
|
|
EXPECT_NE(nullptr, diagnostic);
|
|
|
|
EXPECT_THAT(diagnostic->error, Eq(std::string("Missing assembly text.")));
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, InvalidPointer) {
|
2015-09-10 21:07:21 +00:00
|
|
|
SetText(
|
|
|
|
"OpEntryPoint Kernel 0 \"\"\nOpExecutionMode 0 LocalSizeHint 1 1 1\n");
|
2015-11-12 18:48:30 +00:00
|
|
|
ASSERT_EQ(
|
|
|
|
SPV_ERROR_INVALID_POINTER,
|
|
|
|
spvTextToBinary(context, text.str, text.length, nullptr, &diagnostic));
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, InvalidDiagnostic) {
|
2015-09-10 21:07:21 +00:00
|
|
|
SetText(
|
|
|
|
"OpEntryPoint Kernel 0 \"\"\nOpExecutionMode 0 LocalSizeHint 1 1 1\n");
|
2015-05-22 17:26:19 +00:00
|
|
|
spv_binary binary;
|
|
|
|
ASSERT_EQ(SPV_ERROR_INVALID_DIAGNOSTIC,
|
2015-11-12 18:48:30 +00:00
|
|
|
spvTextToBinary(context, text.str, text.length, &binary, nullptr));
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, InvalidPrefix) {
|
2015-11-12 15:45:36 +00:00
|
|
|
EXPECT_EQ(
|
|
|
|
"Expected <opcode> or <result-id> at the beginning of an instruction, "
|
|
|
|
"found 'Invalid'.",
|
|
|
|
CompileFailure("Invalid"));
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 16:40:59 +00:00
|
|
|
TEST_F(TextToBinaryTest, EmptyAssemblyString) {
|
|
|
|
// An empty assembly module is valid!
|
|
|
|
// It should produce a valid module with zero instructions.
|
|
|
|
EXPECT_THAT(CompiledInstructions(""), Eq(std::vector<uint32_t>{}));
|
|
|
|
}
|
|
|
|
|
2015-05-22 17:26:19 +00:00
|
|
|
TEST_F(TextToBinaryTest, StringSpace) {
|
2015-11-12 15:45:36 +00:00
|
|
|
const std::string code = ("OpSourceExtension \"string with spaces\"\n");
|
|
|
|
EXPECT_EQ(code, EncodeAndDecodeSuccessfully(code));
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
2015-08-21 15:50:55 +00:00
|
|
|
|
Use opcode operand definitions from SPIR-V specification generator.
The assembler and disassembler now use a dynamically adjusted
sequence of expected operand types. (Internally, it is a deque,
for readability.) Both parsers repeatedly pull an expected operand
type from the left of this pattern list, and try to match the next
input token against it.
The expected pattern is adjusted during the parse to accommodate:
- an extended instruction's expected operands, depending on the
extended instruction's index.
- when an operand itself has operands
- to handle sequences of zero or more operands, or pairs of
operands. These are expanded lazily during the parse.
Adds spv::OperandClass from the SPIR-V specification generator.
Modifies spv_operand_desc_t:
- adds hasResult, hasType, and operandClass array to the opcode
description type.
- "wordCount" is replaced with "numTypes", which counts the number
of entries in operandTypes. And each of those describes a
*logical* operand, including the type id for the instruction,
and the result id for the instruction. A logical operand could be
variable-width, such as a literal string.
Adds opcode.inc, an automatically-generated table of operation
descriptions, with one line to describe each core instruction.
Externally, we have modified the SPIR-V spec doc generator to
emit this file.
(We have hacked this copy to use the old semantics for OpLine.)
Inside the assembler, parsing an operand may fail with new
error code SPV_FAIL_MATCH. For an optional operand, this is not
fatal, but should trigger backtracking at a higher level.
The spvTextIsStartOfNewInst checks the case of the third letter
of what might be an opcode. So now, "OpenCL" does not look like
an opcode name.
In assembly, the EntryPoint name field is mandatory, but can be
an empty string.
Adjust tests for changes to:
- OpSampedImage
- OpTypeSampler
2015-08-27 17:03:52 +00:00
|
|
|
TEST_F(TextToBinaryTest, UnknownBeginningOfInstruction) {
|
2015-11-12 15:45:36 +00:00
|
|
|
EXPECT_EQ(
|
2015-08-21 15:50:55 +00:00
|
|
|
"Expected <opcode> or <result-id> at the beginning of an instruction, "
|
|
|
|
"found 'Google'.",
|
2015-11-12 15:45:36 +00:00
|
|
|
CompileFailure(
|
|
|
|
"\nOpSource OpenCL_C 12\nOpMemoryModel Physical64 OpenCL\nGoogle\n"));
|
|
|
|
EXPECT_EQ(4, diagnostic->position.line + 1);
|
|
|
|
EXPECT_EQ(1, diagnostic->position.column + 1);
|
2015-08-21 15:50:55 +00:00
|
|
|
}
|
2015-08-21 15:52:03 +00:00
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, NoEqualSign) {
|
2015-11-12 15:45:36 +00:00
|
|
|
EXPECT_EQ("Expected '=', found end of stream.",
|
|
|
|
CompileFailure("\nOpSource OpenCL_C 12\n"
|
|
|
|
"OpMemoryModel Physical64 OpenCL\n%2\n"));
|
2015-08-21 15:52:03 +00:00
|
|
|
EXPECT_EQ(5, diagnostic->position.line + 1);
|
|
|
|
EXPECT_EQ(1, diagnostic->position.column + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, NoOpCode) {
|
2015-11-12 15:45:36 +00:00
|
|
|
EXPECT_EQ("Expected opcode, found end of stream.",
|
|
|
|
CompileFailure("\nOpSource OpenCL_C 12\n"
|
|
|
|
"OpMemoryModel Physical64 OpenCL\n%2 =\n"));
|
2015-08-21 15:52:03 +00:00
|
|
|
EXPECT_EQ(5, diagnostic->position.line + 1);
|
|
|
|
EXPECT_EQ(1, diagnostic->position.column + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, WrongOpCode) {
|
2015-11-12 15:45:36 +00:00
|
|
|
EXPECT_EQ("Invalid Opcode prefix 'Wahahaha'.",
|
|
|
|
CompileFailure("\nOpSource OpenCL_C 12\n"
|
|
|
|
"OpMemoryModel Physical64 OpenCL\n%2 = Wahahaha\n"));
|
2015-08-21 15:52:03 +00:00
|
|
|
EXPECT_EQ(4, diagnostic->position.line + 1);
|
|
|
|
EXPECT_EQ(6, diagnostic->position.column + 1);
|
|
|
|
}
|
2015-08-31 19:24:32 +00:00
|
|
|
|
2015-09-21 15:36:44 +00:00
|
|
|
using TextToBinaryFloatValueTest = spvtest::TextToBinaryTestBase<
|
2015-09-09 14:36:48 +00:00
|
|
|
::testing::TestWithParam<std::pair<std::string, uint32_t>>>;
|
|
|
|
|
2015-11-06 16:23:57 +00:00
|
|
|
TEST_P(TextToBinaryFloatValueTest, Samples) {
|
|
|
|
const std::string input =
|
|
|
|
"%1 = OpTypeFloat 32\n%2 = OpConstant %1 " + GetParam().first;
|
|
|
|
EXPECT_THAT(CompiledInstructions(input),
|
|
|
|
Eq(Concatenate({MakeInstruction(SpvOpTypeFloat, {1, 32}),
|
|
|
|
MakeInstruction(SpvOpConstant,
|
|
|
|
{1, 2, GetParam().second})})));
|
2015-09-04 18:55:28 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 14:36:48 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(
|
|
|
|
FloatValues, TextToBinaryFloatValueTest,
|
|
|
|
::testing::ValuesIn(std::vector<std::pair<std::string, uint32_t>>{
|
|
|
|
{"0.0", 0x00000000}, // +0
|
|
|
|
{"!0x00000001", 0x00000001}, // +denorm
|
|
|
|
{"!0x00800000", 0x00800000}, // +norm
|
|
|
|
{"1.5", 0x3fc00000},
|
|
|
|
{"!0x7f800000", 0x7f800000}, // +inf
|
|
|
|
{"!0x7f800001", 0x7f800001}, // NaN
|
|
|
|
|
|
|
|
{"-0.0", 0x80000000}, // -0
|
|
|
|
{"!0x80000001", 0x80000001}, // -denorm
|
|
|
|
{"!0x80800000", 0x80800000}, // -norm
|
|
|
|
{"-2.5", 0xc0200000},
|
|
|
|
{"!0xff800000", 0xff800000}, // -inf
|
|
|
|
{"!0xff800001", 0xff800001}, // NaN
|
|
|
|
}));
|
2015-09-04 18:55:28 +00:00
|
|
|
|
2015-10-13 19:51:12 +00:00
|
|
|
TEST(AssemblyContextParseNarrowSignedIntegers, Sample) {
|
|
|
|
AssemblyContext context(AutoText(""), nullptr);
|
2015-10-14 15:31:51 +00:00
|
|
|
const spv_result_t ec = SPV_FAILED_MATCH;
|
2015-10-13 19:51:12 +00:00
|
|
|
int16_t i16;
|
|
|
|
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("", ec, &i16, ""));
|
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("0=", ec, &i16, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("0", ec, &i16, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(0, i16);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("32767", ec, &i16, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(32767, i16);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("-32768", ec, &i16, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(-32768, i16);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("-0", ec, &i16, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(0, i16);
|
|
|
|
|
|
|
|
// These are out of range, so they should return an error.
|
|
|
|
// The error code depends on whether this is an optional value.
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("32768", ec, &i16, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(SPV_ERROR_INVALID_TEXT,
|
2015-10-14 15:31:51 +00:00
|
|
|
context.parseNumber("65535", SPV_ERROR_INVALID_TEXT, &i16, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
|
|
|
|
// Check hex parsing.
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("0x7fff", ec, &i16, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(32767, i16);
|
|
|
|
// This is out of range.
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("0xffff", ec, &i16, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AssemblyContextParseNarrowUnsignedIntegers, Sample) {
|
|
|
|
AssemblyContext context(AutoText(""), nullptr);
|
2015-10-14 15:31:51 +00:00
|
|
|
const spv_result_t ec = SPV_FAILED_MATCH;
|
2015-10-13 19:51:12 +00:00
|
|
|
uint16_t u16;
|
|
|
|
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("", ec, &u16, ""));
|
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("0=", ec, &u16, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("0", ec, &u16, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(0, u16);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("65535", ec, &u16, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(65535, u16);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("65536", ec, &u16, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("-0", ec, &u16, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(0, u16);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("-1", ec, &u16, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(0, u16);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("0xffff", ec, &u16, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(0xffff, u16);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("0x10000", ec, &u16, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AssemblyContextParseWideSignedIntegers, Sample) {
|
|
|
|
AssemblyContext context(AutoText(""), nullptr);
|
2015-10-14 15:31:51 +00:00
|
|
|
const spv_result_t ec = SPV_FAILED_MATCH;
|
2015-10-13 19:51:12 +00:00
|
|
|
int64_t i64;
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("", ec, &i64, ""));
|
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("0=", ec, &i64, ""));
|
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("0", ec, &i64, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(0, i64);
|
|
|
|
EXPECT_EQ(SPV_SUCCESS,
|
2015-10-14 15:31:51 +00:00
|
|
|
context.parseNumber("0x7fffffffffffffff", ec, &i64, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(0x7fffffffffffffff, i64);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("-0", ec, &i64, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(0, i64);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("-1", ec, &i64, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(-1, i64);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AssemblyContextParseWideUnsignedIntegers, Sample) {
|
|
|
|
AssemblyContext context(AutoText(""), nullptr);
|
2015-10-14 15:31:51 +00:00
|
|
|
const spv_result_t ec = SPV_FAILED_MATCH;
|
2015-10-13 19:51:12 +00:00
|
|
|
uint64_t u64;
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("", ec, &u64, ""));
|
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("0=", ec, &u64, ""));
|
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("0", ec, &u64, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(0, u64);
|
|
|
|
EXPECT_EQ(SPV_SUCCESS,
|
2015-10-14 15:31:51 +00:00
|
|
|
context.parseNumber("0xffffffffffffffff", ec, &u64, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(0xffffffffffffffffULL, u64);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("-0", ec, &u64, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(0, u64);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("-1", ec, &u64, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AssemblyContextParseFloat, Sample) {
|
|
|
|
AssemblyContext context(AutoText(""), nullptr);
|
2015-10-14 15:31:51 +00:00
|
|
|
const spv_result_t ec = SPV_FAILED_MATCH;
|
2015-10-13 19:51:12 +00:00
|
|
|
float f;
|
|
|
|
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("", ec, &f, ""));
|
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("0=", ec, &f, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
|
|
|
|
// These values are exactly representatble.
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("0", ec, &f, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(0.0f, f);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("42", ec, &f, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(42.0f, f);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("2.5", ec, &f, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(2.5f, f);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("-32.5", ec, &f, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(-32.5f, f);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("1e38", ec, &f, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(1e38f, f);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("-1e38", ec, &f, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(-1e38f, f);
|
|
|
|
|
|
|
|
// Out of range.
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("1e40", ec, &f, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AssemblyContextParseDouble, Sample) {
|
|
|
|
AssemblyContext context(AutoText(""), nullptr);
|
2015-10-14 15:31:51 +00:00
|
|
|
const spv_result_t ec = SPV_FAILED_MATCH;
|
2015-10-13 19:51:12 +00:00
|
|
|
double f;
|
|
|
|
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("", ec, &f, ""));
|
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("0=", ec, &f, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
|
|
|
|
// These values are exactly representatble.
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("0", ec, &f, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(0.0, f);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("42", ec, &f, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(42.0, f);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("2.5", ec, &f, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(2.5, f);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("-32.5", ec, &f, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(-32.5, f);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("1e38", ec, &f, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(1e38, f);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("-1e38", ec, &f, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(-1e38, f);
|
|
|
|
// These are out of range for 32-bit float, but in range for 64-bit float.
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("1e40", ec, &f, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(1e40, f);
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, context.parseNumber("-1e40", ec, &f, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(-1e40, f);
|
|
|
|
|
|
|
|
// Out of range.
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("1e400", ec, &f, ""));
|
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH, context.parseNumber("-1e400", ec, &f, ""));
|
2015-10-13 19:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AssemblyContextParseMessages, Errors) {
|
|
|
|
spv_diagnostic diag = nullptr;
|
2015-10-14 15:31:51 +00:00
|
|
|
const spv_result_t ec = SPV_FAILED_MATCH;
|
2015-10-13 19:51:12 +00:00
|
|
|
AssemblyContext context(AutoText(""), &diag);
|
|
|
|
int16_t i16;
|
|
|
|
|
|
|
|
// No message is generated for a failure to parse an optional value.
|
2015-11-02 14:41:20 +00:00
|
|
|
EXPECT_EQ(SPV_FAILED_MATCH,
|
|
|
|
context.parseNumber("abc", ec, &i16, "bad narrow int: "));
|
2015-10-13 19:51:12 +00:00
|
|
|
EXPECT_EQ(nullptr, diag);
|
|
|
|
|
|
|
|
// For a required value, use the message fragment.
|
2015-10-14 15:31:51 +00:00
|
|
|
EXPECT_EQ(SPV_ERROR_INVALID_TEXT,
|
|
|
|
context.parseNumber("abc", SPV_ERROR_INVALID_TEXT, &i16,
|
|
|
|
"bad narrow int: "));
|
2015-10-13 19:51:12 +00:00
|
|
|
ASSERT_NE(nullptr, diag);
|
|
|
|
EXPECT_EQ("bad narrow int: abc", std::string(diag->error));
|
|
|
|
// Don't leak.
|
|
|
|
spvDiagnosticDestroy(diag);
|
|
|
|
}
|
|
|
|
|
2015-08-31 19:24:32 +00:00
|
|
|
} // anonymous namespace
|