aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.UnitTesting/Protobuf_TST.cs
blob: bd95efb67106796816fa257f5b253820d7fcde93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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);

        /// <summary>
        /// Compiles all the whole PMR using all available compilers.
        /// </summary>
        [TestMethod]
        public void Compile_All_PMR()
        {
            var console = Helper.InitializeLogging(true);

            String pmrFolder = Helper.GetPMRPath();

            List<String> tempFolders = new List<string>();

            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);
            }
        }

        /// <summary>
        /// Writes and reads a proto message then compares.
        /// </summary>
        [TestMethod]
        public void Read_Write_Message()
        {
            var console = Helper.InitializeLogging(true);

            TangoMessage<JobTicket> container = MessageFactory.CreateTangoMessage<JobTicket>();

            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<JobTicket>(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();
        }

        /// <summary>
        /// Calls a C++ native library using protobuf and gets a result.
        /// </summary>
        [TestMethod]
        public void Call_CPP_And_Get_Result()
        {
            CalculateRequest request = new CalculateRequest();
            request.A = 10;
            request.B = 5;

            NativePMR<CalculateRequest, CalculateResponse> nativePMR = new NativePMR<CalculateRequest, CalculateResponse>(CalculateCPP);
            CalculateResponse response = nativePMR.Invoke(request);

            Assert.AreEqual(response.Sum, request.A + request.B);
        }

        /// <summary>
        /// Calls a C++ native library using protobuf and gets a result.
        /// </summary>
        [TestMethod]
        public void Call_C_And_Get_Result()
        {
            CalculateRequest request = new CalculateRequest();
            request.A = 10;
            request.B = 5;

            NativePMR<CalculateRequest, CalculateResponse> nativePMR = new NativePMR<CalculateRequest, CalculateResponse>(CalculateC);
            CalculateResponse response = nativePMR.Invoke(request);

            Assert.AreEqual(response.Sum, request.A + request.B);
        }
    }
}