| THM1176InstrumentDriver 1.2 C++ API for Metrolab THM1176

Loading...
Searching...
No Matches
TestSCPIParsing.cpp
Go to the documentation of this file.
1// Copyright (c) 2020 Metrolab Technology S.A., Geneva, Switzerland (www.metrolab.com)
2// See the included file LICENSE.txt for the licensing conditions.
3
7
8// Personal includes
9#include "TestSCPIParsing.h"
10#include "SCPIParsing.h"
11#include "VISAInstrumentBuffer.h"
12
13// Standard includes
14#include <cstddef>
15
16 using namespace MTL::SCPI;
17
18// Local Utilities
19 void l_PromptUser(std::string S)
20{
21 std::cout << "PROMPT : " << S << std::endl;
22 std::cin.ignore();
23}
24
25// Mandatory methods
26 void TestSCPIParsing::initTestCase()
27{
28}
29 void TestSCPIParsing::cleanupTestCase()
30{
31 l_PromptUser("Press Enter to quit");
32}
33
35 void TestSCPIParsing::ArbitraryBlock()
36{
37 //----------------------------------------------------------------------//
38 // Arbitrary Blocks //
39 //----------------------------------------------------------------------//
40 #define NOT_ARBITRARY_BLOCK "DUMMY"
41 #define ARBITRARY_BLOCK_INFINITE "#0abcd\0efgh1234#'?@~."
42 #define ARBITRARY_BLOCK_DEFINITE1 "#219abcd\0efgh1234#'?@~.NOTINBLOCK"
43 #define ARBITRARY_BLOCK_DEFINITE2 "#6000019abcd\0efgh1234#'?@~.NOTINBLOCK"
44 #define ARBITRARY_BLOCK_DEFINITE_TOO_SHORT "#6000019abcd\0efgh1234#'?@~"
45 size_t l_Offset, l_Len;
46 //----------------------------------------------------------------------//
47 // IsArbitraryBlock
48 MTL::Instrument::CVISABuffer l_ArbBlockBuff;
49 l_ArbBlockBuff.assign(NOT_ARBITRARY_BLOCK, sizeof(NOT_ARBITRARY_BLOCK) - 1);
50 QVERIFY(!IsArbitraryBlock(l_ArbBlockBuff.begin(), l_ArbBlockBuff.end(), l_Offset, l_Len));
51 l_ArbBlockBuff.assign(ARBITRARY_BLOCK_INFINITE, sizeof(ARBITRARY_BLOCK_INFINITE) - 1);
52 QVERIFY(IsArbitraryBlock(l_ArbBlockBuff.begin(), l_ArbBlockBuff.end(), l_Offset, l_Len) && l_Offset == 2 && l_Len == 19);
53 l_ArbBlockBuff.assign(ARBITRARY_BLOCK_DEFINITE1, sizeof(ARBITRARY_BLOCK_DEFINITE1) - 1);
54 QVERIFY(IsArbitraryBlock(l_ArbBlockBuff.begin(), l_ArbBlockBuff.end(), l_Offset, l_Len) && l_Offset == 4 && l_Len == 19);
55 l_ArbBlockBuff.assign(ARBITRARY_BLOCK_DEFINITE2, sizeof(ARBITRARY_BLOCK_DEFINITE2) - 1);
56 QVERIFY(IsArbitraryBlock(l_ArbBlockBuff.begin(), l_ArbBlockBuff.end(), l_Offset, l_Len) && l_Offset == 8 && l_Len == 19);
57 l_ArbBlockBuff.assign(ARBITRARY_BLOCK_DEFINITE_TOO_SHORT, sizeof(ARBITRARY_BLOCK_DEFINITE_TOO_SHORT) - 1);
58 QVERIFY(!IsArbitraryBlock(l_ArbBlockBuff.begin(), l_ArbBlockBuff.end(), l_Offset, l_Len));
59 //----------------------------------------------------------------------//
60 // ToArbitraryBlock
61 #define RAW_DATA_BLOCK "abcd\0efgh1234#'?@~."
62 #define ARBITRARY_BLOCK_DEFINITE "#219abcd\0efgh1234#'?@~."
63 std::string l_RawData, l_sArbBlock;
64 l_RawData.assign(RAW_DATA_BLOCK, sizeof(RAW_DATA_BLOCK) - 1);
65 ToArbitraryBlock(l_RawData, l_sArbBlock, true);
66 QVERIFY(memcmp(l_sArbBlock.data(), ARBITRARY_BLOCK_INFINITE, sizeof(ARBITRARY_BLOCK_INFINITE) - 1) == 0);
67 ToArbitraryBlock(l_RawData, l_sArbBlock, false);
68 QVERIFY(memcmp(l_sArbBlock.data(), ARBITRARY_BLOCK_DEFINITE, sizeof(ARBITRARY_BLOCK_DEFINITE) - 1) == 0);
69}
71 void TestSCPIParsing::StringChannelList()
72{
73 //----------------------------------------------------------------------//
74 // String Channel Lists //
75 //----------------------------------------------------------------------//
76 #define NOT_CHANNEL "(1!2)"
77 #define CHANNEL_EMPTY "(@)"
78 #define CHANNEL_1PORT "(@1)"
79 #define CHANNEL_MULTIPORTS "(@1!2!255!18)"
80 #define CHANNEL_MULTIPORTS2 "(@1!2!255!18,3!8,7)"
81 //----------------------------------------------------------------------//
82 // FromStringChannelList
83 tChannelList l_ChanList;
84 std::string l_sChanlist;
85 l_sChanlist.assign(NOT_CHANNEL, sizeof(NOT_CHANNEL) - 1);
86 FromStringChannelList(l_sChanlist.begin(), l_sChanlist.end(), l_ChanList);
87 QVERIFY(l_ChanList.empty());
88 l_sChanlist.assign(CHANNEL_EMPTY, sizeof(CHANNEL_EMPTY) - 1);
89 FromStringChannelList(l_sChanlist.begin(), l_sChanlist.end(), l_ChanList);
90 QVERIFY(l_ChanList.empty());
91 l_sChanlist.assign(CHANNEL_1PORT, sizeof(CHANNEL_1PORT) - 1);
92 FromStringChannelList(l_sChanlist.begin(), l_sChanlist.end(), l_ChanList);
93 tChannelList ExpectedChanList = { { 1 } };
94 QVERIFY(l_ChanList == ExpectedChanList);
95 l_sChanlist.assign(CHANNEL_MULTIPORTS, sizeof(CHANNEL_MULTIPORTS) - 1);
96 FromStringChannelList(l_sChanlist.begin(), l_sChanlist.end(), l_ChanList);
97 ExpectedChanList = { { 1,2,255,18 } };
98 QVERIFY(l_ChanList == ExpectedChanList);
99 l_sChanlist.assign(CHANNEL_MULTIPORTS2, sizeof(CHANNEL_MULTIPORTS2) - 1);
100 FromStringChannelList(l_sChanlist.begin(), l_sChanlist.end(), l_ChanList);
101 ExpectedChanList = { { 1,2,255,18 },{ 3,8 },{ 7 } };
102 QVERIFY(l_ChanList == ExpectedChanList);
103 //----------------------------------------------------------------------//
104 // ToStringChannelList
105 std::string l_StrChanList;
106 l_ChanList = { {} };
107 ToStringChannelList(l_ChanList, l_StrChanList);
108 QVERIFY(l_StrChanList == std::string(CHANNEL_EMPTY, sizeof(CHANNEL_EMPTY) - 1));
109 l_ChanList = { { 1 } };
110 ToStringChannelList(l_ChanList, l_StrChanList);
111 QVERIFY(l_StrChanList == std::string(CHANNEL_1PORT, sizeof(CHANNEL_1PORT) - 1));
112 l_ChanList = { { 1,2,255,18 } };
113 ToStringChannelList(l_ChanList, l_StrChanList);
114 QVERIFY(l_StrChanList == std::string(CHANNEL_MULTIPORTS, sizeof(CHANNEL_MULTIPORTS) - 1));
115 l_ChanList = { { 1,2,255,18 },{ 3,8 },{ 7 } };
116 ToStringChannelList(l_ChanList, l_StrChanList);
117 QVERIFY(l_StrChanList == std::string(CHANNEL_MULTIPORTS2, sizeof(CHANNEL_MULTIPORTS2) - 1));
118}
120 void TestSCPIParsing::BinaryChannelList()
121{
122 //----------------------------------------------------------------------//
123 // Binary Channel Lists //
124 //----------------------------------------------------------------------//
125 #define BINARY_CHANNELLIST_EMPTY {}
126 #define BINARY_CHANNELLIST_1PORT {1}
127 #define BINARY_CHANNELLIST_MULTI {1,2,255,18}
128 #define BINARY_CHANNELLIST_MULTI2 {1,2,255,18,0,3,8,0,7}
129 //----------------------------------------------------------------------//
130 // FromBinaryChannelList
131 tChannelList l_ChanList;
132 std::vector<unsigned char> l_BinaryChanList;
133 l_BinaryChanList = BINARY_CHANNELLIST_EMPTY;
134 FromBinaryChannelList((char*)l_BinaryChanList.data(), l_BinaryChanList.size(), l_ChanList);
135 QVERIFY(l_ChanList.empty());
136 l_BinaryChanList = BINARY_CHANNELLIST_1PORT;
137 tChannelList ExpectedChanList = { { 1 } };
138 FromBinaryChannelList((char*)l_BinaryChanList.data(), l_BinaryChanList.size(), l_ChanList);
139 QVERIFY(l_ChanList == ExpectedChanList);
140 l_BinaryChanList = BINARY_CHANNELLIST_MULTI;
141 ExpectedChanList = { { 1,2,255,18 } };
142 FromBinaryChannelList((char*)l_BinaryChanList.data(), l_BinaryChanList.size(), l_ChanList);
143 QVERIFY(l_ChanList == ExpectedChanList);
144 l_BinaryChanList = BINARY_CHANNELLIST_MULTI2;
145 ExpectedChanList = { { 1,2,255,18 },{ 3,8 },{ 7 } };
146 FromBinaryChannelList((char*)l_BinaryChanList.data(), l_BinaryChanList.size(), l_ChanList);
147 QVERIFY(l_ChanList == ExpectedChanList);
148 //----------------------------------------------------------------------//
149 // ToBinaryChannelList
150 std::vector<unsigned char> l_ExpectedBinaryChanList;
151 std::vector<char> l_ObtainedBinaryChanList;
152 l_ChanList = {};
153 l_ExpectedBinaryChanList = BINARY_CHANNELLIST_EMPTY;
154 ToBinaryChannelList(l_ChanList, l_ObtainedBinaryChanList);
155 QVERIFY(memcmp(l_ObtainedBinaryChanList.data(), l_ExpectedBinaryChanList.data(), l_ObtainedBinaryChanList.size()) == 0);
156 l_ChanList = { { 1 } };
157 l_ExpectedBinaryChanList = BINARY_CHANNELLIST_1PORT;
158 ToBinaryChannelList(l_ChanList, l_ObtainedBinaryChanList);
159 QVERIFY(memcmp(l_ObtainedBinaryChanList.data(), l_ExpectedBinaryChanList.data(), l_ObtainedBinaryChanList.size()) == 0);
160 l_ChanList = { { 1,2,255,18 } };
161 l_ExpectedBinaryChanList = BINARY_CHANNELLIST_MULTI;
162 ToBinaryChannelList(l_ChanList, l_ObtainedBinaryChanList);
163 QVERIFY(memcmp(l_ObtainedBinaryChanList.data(), l_ExpectedBinaryChanList.data(), l_ObtainedBinaryChanList.size()) == 0);
164 l_ChanList = { { 1,2,255,18 },{ 3,8 },{ 7 } };
165 l_ExpectedBinaryChanList = BINARY_CHANNELLIST_MULTI2;
166 ToBinaryChannelList(l_ChanList, l_ObtainedBinaryChanList);
167 QVERIFY(memcmp(l_ObtainedBinaryChanList.data(), l_ExpectedBinaryChanList.data(), l_ObtainedBinaryChanList.size()) == 0);
168}
169
170// Test main
171QTEST_MAIN(TestSCPIParsing)
SCPIParsing.h
Utilities to aid in sending SCPI commands and parsing of SCPI reponses.
CHANNEL_MULTIPORTS2
#define CHANNEL_MULTIPORTS2
NOT_ARBITRARY_BLOCK
#define NOT_ARBITRARY_BLOCK
CHANNEL_MULTIPORTS
#define CHANNEL_MULTIPORTS
CHANNEL_EMPTY
#define CHANNEL_EMPTY
NOT_CHANNEL
#define NOT_CHANNEL
CHANNEL_1PORT
#define CHANNEL_1PORT
ARBITRARY_BLOCK_DEFINITE2
#define ARBITRARY_BLOCK_DEFINITE2
ARBITRARY_BLOCK_DEFINITE
#define ARBITRARY_BLOCK_DEFINITE
RAW_DATA_BLOCK
#define RAW_DATA_BLOCK
BINARY_CHANNELLIST_MULTI2
#define BINARY_CHANNELLIST_MULTI2
BINARY_CHANNELLIST_MULTI
#define BINARY_CHANNELLIST_MULTI
ARBITRARY_BLOCK_INFINITE
#define ARBITRARY_BLOCK_INFINITE
BINARY_CHANNELLIST_EMPTY
#define BINARY_CHANNELLIST_EMPTY
ARBITRARY_BLOCK_DEFINITE1
#define ARBITRARY_BLOCK_DEFINITE1
ARBITRARY_BLOCK_DEFINITE_TOO_SHORT
#define ARBITRARY_BLOCK_DEFINITE_TOO_SHORT
l_PromptUser
void l_PromptUser(std::string S)
Definition TestSCPIParsing.cpp:19
BINARY_CHANNELLIST_1PORT
#define BINARY_CHANNELLIST_1PORT
TestSCPIParsing.h
Interface definition for SCPI QtTest object.
TestSCPIParsing
Test the SCPI parsing functions.
Definition TestSCPIParsing.h:15
MTL::SCPI
Definition SCPIParsing.h:20
MTL::SCPI::FromBinaryChannelList
void FromBinaryChannelList(const char *pBinaryChanList, size_t Len, tChannelList &rChannelList)
Decode binary channel list.
Definition SCPIParsing.h:221
MTL::SCPI::ToStringChannelList
void ToStringChannelList(const tChannelList &rChannelList, std::string &rStr)
Encode channel list string.
Definition SCPIParsing.h:201
MTL::SCPI::tChannelList
std::vector< tChannel > tChannelList
SCPI channel list
Definition SCPIParsing.h:29
MTL::SCPI::FromStringChannelList
void FromStringChannelList(iterator_type first, iterator_type last, tChannelList &rChannelList)
Decode channel list string.
Definition SCPIParsing.h:155
MTL::SCPI::ToArbitraryBlock
void ToArbitraryBlock(const std::string &rStr, std::string &rArbitraryBlock, bool InfiniteFormat=false)
Package data as an arbitrary block.
Definition SCPIParsing.h:142
MTL::SCPI::IsArbitraryBlock
bool IsArbitraryBlock(const iterator_type first, const iterator_type last, size_t &rStartOffset, size_t &rLength)
Find arbitrary-block data within a buffer.
Definition SCPIParsing.h:87
MTL::SCPI::ToBinaryChannelList
void ToBinaryChannelList(const tChannelList &rChannelList, std::vector< char > &rBinaryChanList)
Encode binary channel list.
Definition SCPIParsing.h:238