// 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/lang/spirv/writer/binary_writer.h"
#include "gtest/gtest.h"

namespace tint::spirv::writer {
namespace {

using SpirvWriterBinaryWriterTest = testing::Test;

TEST_F(SpirvWriterBinaryWriterTest, Preamble) {
    BinaryWriter bw;
    bw.WriteHeader(5);

    auto res = bw.Result();
    ASSERT_EQ(res.size(), 5u);
    EXPECT_EQ(res[0], spv::MagicNumber);
    EXPECT_EQ(res[1], 0x00010300u);  // SPIR-V 1.3
    EXPECT_EQ(res[2], 23u << 16);    // Generator ID
    EXPECT_EQ(res[3], 5u);           // ID Bound
    EXPECT_EQ(res[4], 0u);           // Reserved
}

TEST_F(SpirvWriterBinaryWriterTest, Float) {
    Module m;

    m.PushAnnot(spv::Op::OpKill, {Operand(2.4f)});
    BinaryWriter bw;
    bw.WriteModule(&m);

    auto res = bw.Result();
    ASSERT_EQ(res.size(), 2u);
    float f;
    memcpy(&f, res.data() + 1, 4);
    EXPECT_EQ(f, 2.4f);
}

TEST_F(SpirvWriterBinaryWriterTest, Int) {
    Module m;

    m.PushAnnot(spv::Op::OpKill, {Operand(2u)});
    BinaryWriter bw;
    bw.WriteModule(&m);

    auto res = bw.Result();
    ASSERT_EQ(res.size(), 2u);
    EXPECT_EQ(res[1], 2u);
}

TEST_F(SpirvWriterBinaryWriterTest, String) {
    Module m;

    m.PushAnnot(spv::Op::OpKill, {Operand("my_string")});
    BinaryWriter bw;
    bw.WriteModule(&m);

    auto res = bw.Result();
    ASSERT_EQ(res.size(), 4u);

    uint8_t* v = reinterpret_cast<uint8_t*>(res.data() + 1);
    EXPECT_EQ(v[0], 'm');
    EXPECT_EQ(v[1], 'y');
    EXPECT_EQ(v[2], '_');
    EXPECT_EQ(v[3], 's');
    EXPECT_EQ(v[4], 't');
    EXPECT_EQ(v[5], 'r');
    EXPECT_EQ(v[6], 'i');
    EXPECT_EQ(v[7], 'n');
    EXPECT_EQ(v[8], 'g');
    EXPECT_EQ(v[9], '\0');
    EXPECT_EQ(v[10], '\0');
    EXPECT_EQ(v[11], '\0');
}

TEST_F(SpirvWriterBinaryWriterTest, String_Multiple4Length) {
    Module m;

    m.PushAnnot(spv::Op::OpKill, {Operand("mystring")});
    BinaryWriter bw;
    bw.WriteModule(&m);

    auto res = bw.Result();
    ASSERT_EQ(res.size(), 4u);

    uint8_t* v = reinterpret_cast<uint8_t*>(res.data() + 1);
    EXPECT_EQ(v[0], 'm');
    EXPECT_EQ(v[1], 'y');
    EXPECT_EQ(v[2], 's');
    EXPECT_EQ(v[3], 't');
    EXPECT_EQ(v[4], 'r');
    EXPECT_EQ(v[5], 'i');
    EXPECT_EQ(v[6], 'n');
    EXPECT_EQ(v[7], 'g');
    EXPECT_EQ(v[8], '\0');
    EXPECT_EQ(v[9], '\0');
    EXPECT_EQ(v[10], '\0');
    EXPECT_EQ(v[11], '\0');
}

TEST_F(SpirvWriterBinaryWriterTest, TestInstructionWriter) {
    Instruction i1{spv::Op::OpKill, {Operand(2u)}};
    Instruction i2{spv::Op::OpKill, {Operand(4u)}};

    BinaryWriter bw;
    bw.WriteInstruction(i1);
    bw.WriteInstruction(i2);

    auto res = bw.Result();
    ASSERT_EQ(res.size(), 4u);
    EXPECT_EQ(res[1], 2u);
    EXPECT_EQ(res[3], 4u);
}

}  // namespace
}  // namespace tint::spirv::writer
