1 /*
2 * Copyright 2022 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "fcp/aggregation/tensorflow/tensorflow_checkpoint_parser_factory.h"
18
19 #include <memory>
20 #include <string>
21 #include <utility>
22
23 #include "gmock/gmock.h"
24 #include "gtest/gtest.h"
25 #include "absl/status/statusor.h"
26 #include "absl/strings/cord.h"
27 #include "fcp/aggregation/protocol/checkpoint_parser.h"
28 #include "fcp/aggregation/testing/testing.h"
29 #include "fcp/base/platform.h"
30 #include "fcp/tensorflow/status.h"
31 #include "fcp/testing/testing.h"
32
33 namespace fcp::aggregation::tensorflow {
34 namespace {
35
TEST(TensorflowCheckpointParserFactoryTest,ReadCheckpoint)36 TEST(TensorflowCheckpointParserFactoryTest, ReadCheckpoint) {
37 std::string filename = TemporaryTestFile(".ckpt");
38 ASSERT_OK(ConvertFromTensorFlowStatus(CreateTfCheckpoint(
39 filename, {"t1", "t2"}, {{1.0f, 2.0f, 3.0f, 4.0f}, {5.0f, 6.0f}})));
40 absl::StatusOr<absl::Cord> checkpoint = ReadFileToCord(filename);
41 ASSERT_OK(checkpoint.status());
42
43 TensorflowCheckpointParserFactory factory;
44 absl::StatusOr<std::unique_ptr<CheckpointParser>> parser =
45 factory.Create(*checkpoint);
46 ASSERT_OK(parser.status());
47
48 auto t1 = (*parser)->GetTensor("t1");
49 ASSERT_OK(t1.status());
50 EXPECT_THAT(*t1, IsTensor<float>({4}, {1.0, 2.0, 3.0, 4.0}));
51 auto t2 = (*parser)->GetTensor("t2");
52 ASSERT_OK(t2.status());
53 EXPECT_THAT(*t2, IsTensor<float>({2}, {5.0, 6.0}));
54 EXPECT_FALSE((*parser)->GetTensor("t3").ok());
55 }
56
TEST(TensorflowCheckpointParserFactoryTest,InvalidCheckpoint)57 TEST(TensorflowCheckpointParserFactoryTest, InvalidCheckpoint) {
58 TensorflowCheckpointParserFactory factory;
59 EXPECT_FALSE(factory.Create(absl::Cord("invalid")).ok());
60 }
61
62 } // namespace
63 } // namespace fcp::aggregation::tensorflow
64