|
| 1 | +/* Copyright 2025 The OpenXLA Authors. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +#include "xla/pjrt/pjrt_common.h" |
| 17 | + |
| 18 | +#include <cstdint> |
| 19 | +#include <string> |
| 20 | +#include <variant> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +#include <gmock/gmock.h> |
| 24 | +#include <gtest/gtest.h> |
| 25 | +#include "xla/pjrt/proto/pjrt_value_type.pb.h" |
| 26 | + |
| 27 | +using testing::ElementsAre; |
| 28 | + |
| 29 | +namespace { |
| 30 | + |
| 31 | +TEST(PjRtCommonTest, PjRtValueTypeFromProtoDefault) { |
| 32 | + xla::PjRtValueTypeProto proto; |
| 33 | + xla::PjRtValueType value = xla::PjRtValueTypeFromProto(proto); |
| 34 | + ASSERT_TRUE(std::holds_alternative<std::string>(value)); |
| 35 | + EXPECT_EQ(std::get<std::string>(value), ""); |
| 36 | +} |
| 37 | + |
| 38 | +TEST(PjRtCommonTest, PjRtValueTypeToProtoFloat) { |
| 39 | + xla::PjRtValueType value = 1.0f; |
| 40 | + xla::PjRtValueTypeProto proto = xla::PjRtValueTypeToProto(value); |
| 41 | + ASSERT_TRUE(proto.has_float_value()); |
| 42 | + EXPECT_EQ(proto.float_value(), 1.0f); |
| 43 | +} |
| 44 | + |
| 45 | +TEST(PjRtCommonTest, PjRtValueTypeToProtoInt) { |
| 46 | + xla::PjRtValueType value = 1L; |
| 47 | + xla::PjRtValueTypeProto proto = xla::PjRtValueTypeToProto(value); |
| 48 | + ASSERT_TRUE(proto.has_int_value()); |
| 49 | + EXPECT_EQ(proto.int_value(), 1L); |
| 50 | +} |
| 51 | + |
| 52 | +TEST(PjRtCommonTest, PjRtValueTypeToProtoString) { |
| 53 | + xla::PjRtValueType value = std::string("test"); |
| 54 | + xla::PjRtValueTypeProto proto = xla::PjRtValueTypeToProto(value); |
| 55 | + ASSERT_TRUE(proto.has_string_value()); |
| 56 | + EXPECT_EQ(proto.string_value(), "test"); |
| 57 | +} |
| 58 | + |
| 59 | +TEST(PjRtCommonTest, PjRtValueTypeToProtoBool) { |
| 60 | + xla::PjRtValueType value = true; |
| 61 | + xla::PjRtValueTypeProto proto = xla::PjRtValueTypeToProto(value); |
| 62 | + ASSERT_TRUE(proto.has_bool_value()); |
| 63 | + EXPECT_EQ(proto.bool_value(), true); |
| 64 | +} |
| 65 | + |
| 66 | +TEST(PjRtCommonTest, PjRtValueTypeToProtoIntVector) { |
| 67 | + xla::PjRtValueType value = std::vector<int64_t>{1, 2, 3}; |
| 68 | + xla::PjRtValueTypeProto proto = xla::PjRtValueTypeToProto(value); |
| 69 | + ASSERT_TRUE(proto.has_int_vector()); |
| 70 | + EXPECT_THAT(proto.int_vector().values(), ElementsAre(1, 2, 3)); |
| 71 | +} |
| 72 | + |
| 73 | +TEST(PjRtCommonTest, PjRtValueTypeFromProtoFloat) { |
| 74 | + xla::PjRtValueTypeProto proto; |
| 75 | + proto.set_float_value(1.0f); |
| 76 | + xla::PjRtValueType value = xla::PjRtValueTypeFromProto(proto); |
| 77 | + ASSERT_TRUE(std::holds_alternative<float>(value)); |
| 78 | + EXPECT_EQ(std::get<float>(value), 1.0f); |
| 79 | +} |
| 80 | + |
| 81 | +TEST(PjRtCommonTest, PjRtValueTypeFromProtoInt) { |
| 82 | + xla::PjRtValueTypeProto proto; |
| 83 | + proto.set_int_value(1L); |
| 84 | + xla::PjRtValueType value = xla::PjRtValueTypeFromProto(proto); |
| 85 | + ASSERT_TRUE(std::holds_alternative<int64_t>(value)); |
| 86 | + EXPECT_EQ(std::get<int64_t>(value), 1L); |
| 87 | +} |
| 88 | + |
| 89 | +TEST(PjRtCommonTest, PjRtValueTypeFromProtoString) { |
| 90 | + xla::PjRtValueTypeProto proto; |
| 91 | + proto.set_string_value("test"); |
| 92 | + xla::PjRtValueType value = xla::PjRtValueTypeFromProto(proto); |
| 93 | + ASSERT_TRUE(std::holds_alternative<std::string>(value)); |
| 94 | + EXPECT_EQ(std::get<std::string>(value), "test"); |
| 95 | +} |
| 96 | + |
| 97 | +TEST(PjRtCommonTest, PjRtValueTypeFromProtoBool) { |
| 98 | + xla::PjRtValueTypeProto proto; |
| 99 | + proto.set_bool_value(true); |
| 100 | + xla::PjRtValueType value = xla::PjRtValueTypeFromProto(proto); |
| 101 | + ASSERT_TRUE(std::holds_alternative<bool>(value)); |
| 102 | + EXPECT_EQ(std::get<bool>(value), true); |
| 103 | +} |
| 104 | + |
| 105 | +TEST(PjRtCommonTest, PjRtValueTypeFromProtoIntVector) { |
| 106 | + xla::PjRtValueTypeProto proto; |
| 107 | + proto.mutable_int_vector()->add_values(1); |
| 108 | + proto.mutable_int_vector()->add_values(2); |
| 109 | + proto.mutable_int_vector()->add_values(3); |
| 110 | + xla::PjRtValueType value = xla::PjRtValueTypeFromProto(proto); |
| 111 | + ASSERT_TRUE(std::holds_alternative<std::vector<int64_t>>(value)); |
| 112 | + EXPECT_THAT(std::get<std::vector<int64_t>>(value), |
| 113 | + testing::ElementsAre(1, 2, 3)); |
| 114 | +} |
| 115 | + |
| 116 | +} // namespace |
0 commit comments