aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.UnitTesting/Protobuf_TST.cs
blob: 355448a5c8eec2e486a388820b0cb84013f5ed7b (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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.Jobs;
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<Job> container = MessageFactory.CreateTangoMessage<Job>();

            container.Message.Name = "Test Job";

            container.Message.Segments.Add(new Segment()
            {
                Color = new RGB() { R = 1, G = 2, B = 3 },
                Length = 10,
                Name = "Segment 1"
            });

            container.Message.Segments.Add(new Segment()
            {
                Color = new RGB() { R = 10, G = 20, B = 30 },
                Length = 100,
                Name = "Segment 2"
            });

            LogManager.Log("Write Message:" + Environment.NewLine + JsonConvert.SerializeObject(container.Message, Formatting.Indented));

            var bytes = container.ToBytes();

            var parsed = MessageFactory.ParseTangoMessage<Job>(bytes);

            LogManager.Log("Read Message:" + Environment.NewLine + JsonConvert.SerializeObject(parsed.Message, Formatting.Indented));

            Assert.AreEqual(container.Message, parsed.Message);

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