// Copyright 2024 The Dawn & Tint Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
//    contributors may be used to endorse or promote products derived from
//    this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "gmock/gmock.h"
#include "src/tint/lang/glsl/writer/helper_test.h"

namespace tint::glsl::writer {
namespace {

using namespace tint::core::fluent_types;     // NOLINT
using namespace tint::core::number_suffixes;  // NOLINT

TEST_F(GlslWriterTest, Constructor_Type_Float_Large) {
    auto* func = b.Function("a", ty.f32());
    b.Append(func->Block(), [&] {  //
        // Use a number close to 1<<30 but whose decimal representation ends in 0.
        b.Return(func, b.Construct(ty.f32(), f32((1 << 30) - 4)));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
float a() {
  return float(1073741824.0f);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  float x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Float) {
    auto* func = b.Function("a", ty.f32());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Construct(ty.f32(), -1.2e-5_f));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
float a() {
  return float(-0.00001200000042445026f);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  float x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_F16_Large) {
    auto* func = b.Function("a", ty.f16());
    b.Append(func->Block(), [&] {  //
        // Use a number close to 1<<16 but whose decimal representation ends in 0.
        b.Return(func, b.Construct(ty.f16(), f16((1 << 15) - 8)));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require

float16_t a() {
  return float16_t(32752.0hf);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  float16_t x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_F16) {
    auto* func = b.Function("a", ty.f16());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Construct(ty.f16(), -1.2e-3_h));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require

float16_t a() {
  return float16_t(-0.0011997222900390625hf);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  float16_t x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Bool_False) {
    auto* func = b.Function("a", ty.bool_());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Construct(ty.bool_(), false));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
bool a() {
  return bool(false);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  bool x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Bool_True) {
    auto* func = b.Function("a", ty.bool_());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Construct(ty.bool_(), true));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
bool a() {
  return bool(true);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  bool x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Int) {
    auto* func = b.Function("a", ty.i32());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Construct(ty.i32(), -12345_i));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
int a() {
  return int(-12345);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  int x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Uint) {
    auto* func = b.Function("a", ty.u32());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Construct(ty.u32(), 12345_u));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
uint a() {
  return uint(12345u);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  uint x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Vec_F32) {
    auto* func = b.Function("a", ty.vec3f());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Construct(ty.vec3f(), 1.0_f, 2.0_f, 3.0_f));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
vec3 a() {
  return vec3(1.0f, 2.0f, 3.0f);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  vec3 x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Vec_F16) {
    auto* func = b.Function("a", ty.vec3h());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Construct(ty.vec3h(), 1_h, 2_h, 3_h));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require

f16vec3 a() {
  return f16vec3(1.0hf, 2.0hf, 3.0hf);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  f16vec3 x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Vec_Empty_F32) {
    auto* func = b.Function("a", ty.vec3f());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Construct(ty.vec3f()));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
vec3 a() {
  return vec3(0.0f);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  vec3 x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Vec_Empty_F16) {
    auto* func = b.Function("a", ty.vec3h());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Construct(ty.vec3h()));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require

f16vec3 a() {
  return f16vec3(0.0hf);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  f16vec3 x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Vec_SingleScalar_F32_Literal) {
    auto* func = b.Function("a", ty.vec3f());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Splat(ty.vec3f(), 2_f));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
vec3 a() {
  return vec3(2.0f);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  vec3 x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Vec_SingleScalar_F16_Literal) {
    auto* func = b.Function("a", ty.vec3h());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Splat(ty.vec3h(), 2_h));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require

f16vec3 a() {
  return f16vec3(2.0hf);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  f16vec3 x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Vec_SingleScalar_F32_Var) {
    auto* func = b.Function("a", ty.vec3f());
    b.Append(func->Block(), [&] {  //
        auto* v = b.Var("v", 2_f);
        b.Return(func, b.Construct(ty.vec3f(), b.Load(v)));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
vec3 a() {
  float v = 2.0f;
  return vec3(v);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  vec3 x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Vec_SingleScalar_F16_Var) {
    auto* func = b.Function("a", ty.vec3h());
    b.Append(func->Block(), [&] {  //
        auto* v = b.Var("v", 2_h);
        b.Return(func, b.Construct(ty.vec3h(), b.Load(v)));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require

f16vec3 a() {
  float16_t v = 2.0hf;
  return f16vec3(v);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  f16vec3 x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Vec_SingleScalar_Bool) {
    auto* func = b.Function("a", ty.vec3<bool>());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Splat(ty.vec3<bool>(), true));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
bvec3 a() {
  return bvec3(true);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  bvec3 x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Vec_SingleScalar_Int) {
    auto* func = b.Function("a", ty.vec3i());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Splat(ty.vec3i(), 2_i));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
ivec3 a() {
  return ivec3(2);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  ivec3 x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Vec_SingleScalar_UInt) {
    auto* func = b.Function("a", ty.vec3u());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Construct(ty.vec3u(), 2_u));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
uvec3 a() {
  return uvec3(2u);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  uvec3 x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Mat_F32) {
    auto* func = b.Function("a", ty.mat2x3<f32>());
    b.Append(func->Block(), [&] {  //
        auto* v1 = b.Let("v1", b.Construct(ty.vec3f(), 1_f, 2_f, 3_f));
        auto* v2 = b.Let("v2", b.Construct(ty.vec3f(), 3_f, 4_f, 5_f));
        b.Return(func, b.Construct(ty.mat2x3<f32>(), v1, v2));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
mat2x3 a() {
  vec3 v1 = vec3(1.0f, 2.0f, 3.0f);
  vec3 v2 = vec3(3.0f, 4.0f, 5.0f);
  return mat2x3(v1, v2);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  mat2x3 x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Mat_F16) {
    auto* func = b.Function("a", ty.mat2x3<f16>());
    b.Append(func->Block(), [&] {  //
        auto* v1 = b.Let("v1", b.Construct(ty.vec3h(), 1_h, 2_h, 3_h));
        auto* v2 = b.Let("v2", b.Construct(ty.vec3h(), 3_h, 4_h, 5_h));
        b.Return(func, b.Construct(ty.mat2x3<f16>(), v1, v2));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require

f16mat2x3 a() {
  f16vec3 v1 = f16vec3(1.0hf, 2.0hf, 3.0hf);
  f16vec3 v2 = f16vec3(3.0hf, 4.0hf, 5.0hf);
  return f16mat2x3(v1, v2);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  f16mat2x3 x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Mat_Complex_F32) {
    // mat4x4<f32>(
    //     vec4<f32>(2.0f, 3.0f, 4.0f, 8.0f),
    //     vec4<f32>(),
    //     vec4<f32>(7.0f),
    //     vec4<f32>(vec4<f32>(42.0f, 21.0f, 6.0f, -5.0f)),
    //   );
    auto* func = b.Function("a", ty.mat4x4<f32>());
    b.Append(func->Block(), [&] {  //
        auto* v1 = b.Construct(ty.vec4f(), 2.0_f, 3.0_f, 4.0_f, 8.0_f);
        auto* v2 = b.Construct(ty.vec4f());
        auto* v3 = b.Splat(ty.vec4f(), 7.0_f);
        auto* v4 = b.Construct(ty.vec4f(), b.Construct(ty.vec4f(), 42.0_f, 21.0_f, 6.0_f, -5.0_f));
        b.Return(func, b.Construct(ty.mat4x4<f32>(), v1, v2, v3, v4));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
mat4 a() {
  vec4 v = vec4(2.0f, 3.0f, 4.0f, 8.0f);
  vec4 v_1 = vec4(0.0f);
  return mat4(v, v_1, vec4(7.0f), vec4(vec4(42.0f, 21.0f, 6.0f, -5.0f)));
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  mat4 x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Mat_Complex_F16) {
    // mat4x4<f16>(
    //     vec4<f16>(2.0h, 3.0h, 4.0h, 8.0h),
    //     vec4<f16>(),
    //     vec4<f16>(7.0h),
    //     vec4<f16>(vec4<f16>(42.0h, 21.0h, 6.0h, -5.0h)),
    //   );
    auto* func = b.Function("a", ty.mat4x4<f16>());
    b.Append(func->Block(), [&] {  //
        auto* v1 = b.Construct(ty.vec4h(), 2.0_h, 3.0_h, 4.0_h, 8.0_h);
        auto* v2 = b.Construct(ty.vec4h());
        auto* v3 = b.Splat(ty.vec4h(), 7.0_h);
        auto* v4 = b.Construct(ty.vec4h(), b.Construct(ty.vec4h(), 42.0_h, 21.0_h, 6.0_h, -5.0_h));
        b.Return(func, b.Construct(ty.mat4x4<f16>(), v1, v2, v3, v4));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require

f16mat4 a() {
  f16vec4 v = f16vec4(2.0hf, 3.0hf, 4.0hf, 8.0hf);
  f16vec4 v_1 = f16vec4(0.0hf);
  return f16mat4(v, v_1, f16vec4(7.0hf), f16vec4(f16vec4(42.0hf, 21.0hf, 6.0hf, -5.0hf)));
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  f16mat4 x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Mat_Empty_F32) {
    auto* func = b.Function("a", ty.mat2x3<f32>());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Construct(ty.mat2x3<f32>()));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
mat2x3 a() {
  return mat2x3(vec3(0.0f), vec3(0.0f));
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  mat2x3 x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Mat_Empty_F16) {
    auto* func = b.Function("a", ty.mat2x3<f16>());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Construct(ty.mat2x3<f16>()));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require

f16mat2x3 a() {
  return f16mat2x3(f16vec3(0.0hf), f16vec3(0.0hf));
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  f16mat2x3 x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Mat_Identity_F32) {
    // fn f() {
    //     var m_1: mat4x4<f32> = mat4x4<f32>();
    //     var m_2: mat4x4<f32> = mat4x4<f32>(m_1);
    // }

    auto* func = b.Function("a", ty.mat4x4<f32>());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Construct(ty.mat4x4<f32>(), b.Construct(ty.mat4x4<f32>())));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
mat4 a() {
  return mat4(mat4(vec4(0.0f), vec4(0.0f), vec4(0.0f), vec4(0.0f)));
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  mat4 x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Mat_Identity_F16) {
    // fn f() {
    //     var m_1: mat4x4<f16> = mat4x4<f16>();
    //     var m_2: mat4x4<f16> = mat4x4<f16>(m_1);
    // }

    auto* func = b.Function("a", ty.mat4x4<f16>());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Construct(ty.mat4x4<f16>(), b.Construct(ty.mat4x4<f16>())));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(#extension GL_AMD_gpu_shader_half_float: require

f16mat4 a() {
  return f16mat4(f16mat4(f16vec4(0.0hf), f16vec4(0.0hf), f16vec4(0.0hf), f16vec4(0.0hf)));
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  f16mat4 x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Array) {
    auto* func = b.Function("a", ty.array<vec3<f32>, 3>());
    b.Append(func->Block(), [&] {  //
        auto* v1 = b.Let("v1", b.Construct(ty.vec3f(), 1_f, 2_f, 3_f));
        auto* v2 = b.Let("v2", b.Construct(ty.vec3f(), 4_f, 5_f, 6_f));
        auto* v3 = b.Let("v3", b.Construct(ty.vec3f(), 7_f, 8_f, 9_f));
        b.Return(func, b.Construct(ty.array<vec3<f32>, 3>(), v1, v2, v3));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
vec3[3] a() {
  vec3 v1 = vec3(1.0f, 2.0f, 3.0f);
  vec3 v2 = vec3(4.0f, 5.0f, 6.0f);
  vec3 v3 = vec3(7.0f, 8.0f, 9.0f);
  return vec3[3](v1, v2, v3);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  vec3 x[3] = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Array_Empty) {
    auto* func = b.Function("a", ty.array<vec3<f32>, 3>());
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Construct(ty.array<vec3<f32>, 3>()));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
vec3[3] a() {
  return vec3[3](vec3(0.0f), vec3(0.0f), vec3(0.0f));
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  vec3 x[3] = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Struct) {
    auto* str = ty.Struct(mod.symbols.New("S"), {
                                                    {mod.symbols.New("a"), ty.i32()},
                                                    {mod.symbols.New("b"), ty.f32()},
                                                    {mod.symbols.New("c"), ty.vec3i()},
                                                });

    auto* func = b.Function("a", str);
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Construct(str, 1_i, 2_f, b.Construct(ty.vec3i(), 3_i, 4_i, 5_i)));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(

struct S {
  int a;
  float b;
  ivec3 c;
};

S a() {
  return S(1, 2.0f, ivec3(3, 4, 5));
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  S x = a();
}
)");
}

TEST_F(GlslWriterTest, Constructor_Type_Struct_Empty) {
    auto* str = ty.Struct(mod.symbols.New("S"), {
                                                    {mod.symbols.New("a"), ty.i32()},
                                                    {mod.symbols.New("b"), ty.f32()},
                                                    {mod.symbols.New("c"), ty.vec3i()},
                                                });

    auto* func = b.Function("a", str);
    b.Append(func->Block(), [&] {  //
        b.Return(func, b.Construct(str));
    });

    auto* eb = b.ComputeFunction("main");
    b.Append(eb->Block(), [&] {
        b.Let("x", b.Call(func));
        b.Return(eb);
    });

    auto result = Generate();
    ASSERT_EQ(result, Success) << result.Failure().reason << output_.glsl;
    EXPECT_EQ(output_.glsl, GlslHeader() + R"(

struct S {
  int a;
  float b;
  ivec3 c;
};

S a() {
  return S(0, 0.0f, ivec3(0));
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  S x = a();
}
)");
}

}  // namespace
}  // namespace tint::glsl::writer
