using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Tango.Protobuf;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Google.Protobuf;
using Tango.PMR.Printing;
using Tango.PMR.Common;
using Tango.PMR;
using Tango.Logging;
using Newtonsoft.Json;
using System.Runtime.InteropServices;
using Tango.Core.Helpers;
using System.Text;
using Tango.PMR.Stubs;
namespace Tango.UnitTesting
{
[TestClass]
[TestCategory("Protobuf")]
public class Protobuf_TST
{
[DllImport("Tango.ProtoTest.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "Calculate")]
public static extern int CalculateCPP(IntPtr data, int size, ref IntPtr output);
[DllImport("Tango.ColorLib.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "Calculate")]
public static extern int CalculateC(IntPtr data, int size, ref IntPtr output);
///
/// Compiles all the whole PMR using all available compilers.
///
[TestMethod]
public void Compile_All_PMR()
{
var console = Helper.InitializeLogging(true);
String pmrFolder = Helper.GetPMRPath();
List tempFolders = new List();
foreach (var compiler in CompilerFactory.GetAvailableCompilers())
{
CompilerFolderResult result = compiler.CompileFolder(pmrFolder);
String tmpFolder = Helper.GetTempFolderPathAppend(compiler.Language.ToString());
result.Save(tmpFolder);
tempFolders.Add(tmpFolder);
}
Helper.ShowInExplorer(Directory.GetParent(tempFolders.First()).FullName);
console.WaitForConsoleExit().Wait();
foreach (var folder in tempFolders)
{
Helper.TryDeleteFolder(folder);
}
}
///
/// Writes and reads a proto message then compares.
///
[TestMethod]
public void Read_Write_Message()
{
var console = Helper.InitializeLogging(true);
TangoMessage container = MessageFactory.CreateTangoMessage();
container.Message.Name = "Test Job";
container.Message.Segments.Add(new JobSegment()
{
Length = 10,
Name = "Segment 1"
});
container.Message.Segments.Add(new JobSegment()
{
Length = 100,
Name = "Segment 2"
});
LogManager.Default.Log("Write Message:" + Environment.NewLine + JsonConvert.SerializeObject(container.Message, Formatting.Indented));
var bytes = container.ToBytes();
var parsed = MessageFactory.ParseTangoMessage(bytes);
LogManager.Default.Log("Read Message:" + Environment.NewLine + JsonConvert.SerializeObject(parsed.Message, Formatting.Indented));
Assert.AreEqual(container.Message, parsed.Message);
LogManager.Default.Log("Test Passed!");
console.WaitForConsoleExit().Wait();
}
///
/// Calls a C++ native library using protobuf and gets a result.
///
[TestMethod]
public void Call_CPP_And_Get_Result()
{
CalculateRequest request = new CalculateRequest();
request.A = 10;
request.B = 5;
NativePMR nativePMR = new NativePMR(CalculateCPP);
CalculateResponse response = nativePMR.Invoke(request);
Assert.AreEqual(response.Sum, request.A + request.B);
}
///
/// Calls a C++ native library using protobuf and gets a result.
///
[TestMethod]
public void Call_C_And_Get_Result()
{
CalculateRequest request = new CalculateRequest();
request.A = 10;
request.B = 5;
NativePMR nativePMR = new NativePMR(CalculateC);
CalculateResponse response = nativePMR.Invoke(request);
Assert.AreEqual(response.Sum, request.A + request.B);
}
}
}