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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namesp
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);
        }
    }
}