|
| 1 | +#include <string> |
| 2 | + |
| 3 | +#include "file/base/file.h" |
| 4 | +#include "file/base/helpers.h" |
| 5 | +#include "file/base/path.h" |
| 6 | + |
| 7 | +#include "testing/base/public/gmock.h" |
| 8 | +#include "testing/base/public/gunit.h" |
| 9 | +#include "third_party/jsoncpp/json.h" |
| 10 | +#include "third_party/tensorflow/core/framework/fake_input.h" |
| 11 | +#include "third_party/tensorflow/core/framework/node_def_builder.h" |
| 12 | +#include "third_party/tensorflow/core/framework/tensor.h" |
| 13 | +#include "third_party/tensorflow/core/framework/tensor_testutil.h" |
| 14 | +#include "third_party/tensorflow/core/kernels/ops_testutil.h" |
| 15 | +#include "third_party/tensorflow/core/lib/core/status_test_util.h" |
| 16 | + |
| 17 | +namespace morph_net { |
| 18 | + |
| 19 | +using ::tensorflow::DT_INT32; |
| 20 | +using ::tensorflow::FakeInput; |
| 21 | +using ::tensorflow::NodeDefBuilder; |
| 22 | +using ::tensorflow::OpsTestBase; |
| 23 | +using ::tensorflow::Status; |
| 24 | +using ::tensorflow::TensorShape; |
| 25 | +using ::testing::ElementsAre; |
| 26 | + |
| 27 | + |
| 28 | +std::vector<int> ToVector(const Json::Value& json) { |
| 29 | + std::vector<int> v; |
| 30 | + for (const Json::Value& item : json) { |
| 31 | + v.push_back(item.asInt()); |
| 32 | + } |
| 33 | + return v; |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +class JsonTensorExporterTest : public OpsTestBase {}; |
| 38 | + |
| 39 | +TEST_F(JsonTensorExporterTest, Success) { |
| 40 | + const int kLength = 3; |
| 41 | + const string filename = ::file::JoinPath(FLAGS_test_tmpdir, "success.json"); |
| 42 | + TF_ASSERT_OK( |
| 43 | + NodeDefBuilder("exporter", "JsonTensorExporter") |
| 44 | + .Attr("T", DT_INT32) |
| 45 | + .Attr("N", kLength) |
| 46 | + .Attr("keys", {"k1", "k2", "k3"}) |
| 47 | + .Attr("filename", filename) |
| 48 | + .Input(FakeInput(kLength, ::tensorflow::DT_INT32)) |
| 49 | + .Input(FakeInput(::tensorflow::DT_BOOL)) |
| 50 | + .Finalize(node_def())); |
| 51 | + |
| 52 | + TF_ASSERT_OK(InitOp()); |
| 53 | + // The initialization of the op creates an empty file at `filename`. We delete |
| 54 | + // both to verify it was created, and to clean it up for the next steps of the |
| 55 | + // test. |
| 56 | + ASSERT_OK(::file::Delete(filename, ::file::Defaults())); |
| 57 | + |
| 58 | + AddInputFromArray<int>(TensorShape({3}), {3, 5, 7}); |
| 59 | + AddInputFromArray<int>(TensorShape({2}), {6, 4}); |
| 60 | + AddInputFromArray<int>(TensorShape({}), {9}); |
| 61 | + |
| 62 | + // Set the `save` flag initially to false - so the op should be a no-op. |
| 63 | + AddInputFromArray<bool>(TensorShape({}), {false}); |
| 64 | + TF_ASSERT_OK(RunOpKernel()); |
| 65 | + // Verify that indeed no file was created. |
| 66 | + EXPECT_EQ(absl::StatusCode::kNotFound, |
| 67 | + ::file::Exists(filename, ::file::Defaults()).code()); |
| 68 | + |
| 69 | + // Flip the `save` flag to true and test the content of the savef file. |
| 70 | + tensors_[3]->scalar<bool>()() = true; |
| 71 | + TF_ASSERT_OK(RunOpKernel()); |
| 72 | + |
| 73 | + string contents; |
| 74 | + ASSERT_OK(::file::GetContents(filename, &contents, ::file::Defaults())); |
| 75 | + Json::Reader reader; |
| 76 | + Json::Value json; |
| 77 | + reader.parse(contents, json); |
| 78 | + EXPECT_THAT(json.getMemberNames(), ElementsAre("k1", "k2", "k3")); |
| 79 | + EXPECT_TRUE(json["k1"].isArray()); |
| 80 | + EXPECT_THAT(ToVector(json["k1"]), ElementsAre(3, 5, 7)); |
| 81 | + EXPECT_TRUE(json["k2"].isArray()); |
| 82 | + EXPECT_THAT(ToVector(json["k2"]), ElementsAre(6, 4)); |
| 83 | + EXPECT_EQ(9, json["k3"].asInt()); |
| 84 | +} |
| 85 | + |
| 86 | +TEST_F(JsonTensorExporterTest, WrongNumberOfKeys) { |
| 87 | + const int kLength = 3; |
| 88 | + const string filename = ::file::JoinPath(FLAGS_test_tmpdir, "failure.json"); |
| 89 | + TF_ASSERT_OK( |
| 90 | + NodeDefBuilder("exporter", "JsonTensorExporter") |
| 91 | + .Attr("T", DT_INT32) |
| 92 | + .Attr("N", kLength) |
| 93 | + .Attr("keys", {"k1", "k2"}) // Two keys only, even though kLength = 3. |
| 94 | + .Attr("filename", filename) |
| 95 | + .Input(FakeInput(kLength, ::tensorflow::DT_INT32)) |
| 96 | + .Input(FakeInput(::tensorflow::DT_BOOL)) |
| 97 | + .Finalize(node_def())); |
| 98 | + |
| 99 | + EXPECT_FALSE(InitOp().ok()); |
| 100 | +} |
| 101 | + |
| 102 | +TEST_F(JsonTensorExporterTest, BadFileName) { |
| 103 | + const int kLength = 3; |
| 104 | + const string filename = "**bad"; |
| 105 | + TF_ASSERT_OK( |
| 106 | + NodeDefBuilder("exporter", "JsonTensorExporter") |
| 107 | + .Attr("T", DT_INT32) |
| 108 | + .Attr("N", kLength) |
| 109 | + .Attr("keys", {"k1", "k2", "k3"}) |
| 110 | + .Attr("filename", filename) |
| 111 | + .Input(FakeInput(kLength, ::tensorflow::DT_INT32)) |
| 112 | + .Input(FakeInput(::tensorflow::DT_BOOL)) |
| 113 | + .Finalize(node_def())); |
| 114 | + |
| 115 | + EXPECT_FALSE(InitOp().ok()); |
| 116 | +} |
| 117 | + |
| 118 | +} // namespace morph_net |
0 commit comments