/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 *
 * communication.cpp
 *
 * Author: pgess <v.melnychenko@xreate.org>
 * Created on October 14, 2017, 5:24 PM
 */

#include "xreatemanager.h"
#include "supplemental/docutils.h"

#include "gtest/gtest.h"

using namespace xreate;
using namespace std;

TEST(Communication, ast_Basic1){
    FILE* script = fopen("scripts/effects-communication/example1-wr.xreate", "r");
    std::unique_ptr<XreateManager> program(XreateManager::prepare(script));

    ASSERT_TRUE(true);
    fclose(script);
}

TEST(Communication, analysis_Basic1){
    FILE* script = fopen("scripts/effects-communication/example1-wr.xreate", "r");
    std::unique_ptr<details::tier1::XreateManager> program(details::tier1::XreateManager::prepare(script));
    fclose(script);

    program->analyse();
    ASSERT_TRUE(true);
}

TEST(Communication, full_Basic1){
    FILE* script = fopen("scripts/effects-communication/example1-wr.xreate", "r");
    std::unique_ptr<XreateManager> program(XreateManager::prepare(script));
    fclose(script);

    int (*programEntry)() = (int (*)())program->run();
    int result = programEntry();

    ASSERT_EQ(1, result);
}

TEST(Communication, full_Basic2){
    FILE* script = fopen("scripts/effects-communication/example2-wr.xreate", "r");
    std::unique_ptr<XreateManager> program(XreateManager::prepare(script));
    fclose(script);

    int (*programEntry)() = (int (*)())program->run();
    int result = programEntry();

    ASSERT_EQ(1, result);
}

TEST(Communication, analysis_Weak1){
    FILE* script = fopen("scripts/effects-communication/example3-wrwr.xreate", "r");

    std::unique_ptr<details::tier1::XreateManager> program(details::tier1::XreateManager::prepare(script));
    fclose(script);

    program->analyse();
    ASSERT_TRUE(true);
}

TEST(Communication, full_Weak1){
    FILE* script = fopen("scripts/effects-communication/example3-wrwr.xreate", "r");

    std::unique_ptr<XreateManager> program(XreateManager::prepare(script));
    fclose(script);

    int (*programEntry)(int) = (int (*)(int))program->run();

    ASSERT_EQ(1, programEntry(1));
    ASSERT_EQ(-1, programEntry(0));
}

TEST(Communication, Doc_DirImpl){
  string example = getDocumentationExampleById("documentation/communication.xml", "DirImpl_1");
  XreateManager* xreate = XreateManager::prepare(move(example));
  xreate->run();
}

TEST(Communication, Doc_GuardedImpl){
  string example = getDocumentationExampleById("documentation/communication.xml", "GuardedImpl_1");
  XreateManager* xreate = XreateManager::prepare(move(example));
  xreate->run();
}