/**
 * Copyright 2019-2020 Huawei Technologies Co., Ltd
 *
 * 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 <gtest/gtest.h>

#define protected public
#define private public
#include "graph/passes/snapshot_pass.h"

#include "common/op/ge_op_utils.h"
#include "common/types.h"
#include "graph/anchor.h"
#include "graph/attr_value.h"
#include "graph/compute_graph.h"
#include "graph/op_desc.h"
#include "graph/utils/attr_utils.h"
#include "graph/utils/graph_utils.h"
#include "graph/utils/op_desc_utils.h"
#include "graph/utils/tensor_utils.h"
#include "inc/pass_manager.h"
#undef protected
#undef private

using namespace testing;
using namespace std;
using namespace ge;

class UtestSnapshotPass : public Test {
 protected:
  NodePtr AddNode(ComputeGraphPtr graph, const string &name, const string &type, int32_t in_anchors_num = 1,
                  int32_t out_anchors_num = 1) {
    GeTensorDesc tensor_desc;
    OpDescPtr op_desc = make_shared<OpDesc>(name, type);
    for (int32_t i = 0; i < in_anchors_num; i++) {
      op_desc->AddInputDesc(tensor_desc);
    }
    for (int32_t i = 0; i < out_anchors_num; i++) {
      op_desc->AddOutputDesc(tensor_desc);
    }

    NodePtr node = graph->AddNode(op_desc);
    return node;
  }
};

TEST_F(UtestSnapshotPass, succ) {
  ComputeGraphPtr graph = std::make_shared<ComputeGraph>("test");

  NodePtr prevent_grad = AddNode(graph, "PreventGradient", PREVENTGRADIENT);
  NodePtr snapshot = AddNode(graph, "Snapshot", SNAPSHOT);
  NodePtr constant = AddNode(graph, "Const", CONSTANT);
  GraphUtils::AddEdge(prevent_grad->GetOutDataAnchor(0), snapshot->GetInDataAnchor(0));
  GraphUtils::AddEdge(snapshot->GetOutDataAnchor(0), constant->GetInDataAnchor(0));

  SnapshotPass pass;
  Status status = pass.Run(snapshot);
  EXPECT_EQ(status, SUCCESS);

  NodePtr found_node = graph->FindNode("Snapshot");
  EXPECT_EQ(found_node, nullptr);

  status = pass.Run(prevent_grad);
  EXPECT_EQ(status, SUCCESS);

  string type2 = "FrameworkOp";
  snapshot->GetOpDesc()->SetType(type2);
  status = pass.Run(snapshot);
}
