-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathvpi_value_conversion_test.cpp
114 lines (87 loc) · 3.89 KB
/
vpi_value_conversion_test.cpp
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
// -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
#include <stdlib.h>
#include <iostream>
#include <memory>
#include "gtest/gtest.h"
#include "uhdm/vhpi_user.h" // vpi_user functions.
#include "uhdm/vpi_uhdm.h" // struct uhdm_handle
TEST(VpiValue, ToString) {
s_vpi_value value;
value.format = vpiIntVal;
value.value.integer = 42;
EXPECT_EQ(VpiValue2String(&value), "INT:42");
value.format = vpiScalarVal;
value.value.integer = vpiX;
EXPECT_EQ(VpiValue2String(&value), "SCAL:X");
value.format = vpiStringVal;
value.value.str = (PLI_BYTE8 *)"helloworld";
EXPECT_EQ(VpiValue2String(&value), "STRING:helloworld");
value.format = vpiHexStrVal;
value.value.str = (PLI_BYTE8 *)"FEEDCAFE";
EXPECT_EQ(VpiValue2String(&value), "HEX:FEEDCAFE");
value.format = vpiOctStrVal;
value.value.str = (PLI_BYTE8 *)"007";
EXPECT_EQ(VpiValue2String(&value), "OCT:007");
value.format = vpiBinStrVal;
value.value.str = (PLI_BYTE8 *)"101010";
EXPECT_EQ(VpiValue2String(&value), "BIN:101010");
value.format = vpiRealVal;
value.value.real = 3.141592;
EXPECT_EQ(VpiValue2String(&value), "REAL:3.141592");
}
static std::string ParseAndRegenerateString(const std::string &str) {
std::unique_ptr<s_vpi_value> val(String2VpiValue(str));
return VpiValue2String(val.get());
}
static bool ParseConvertBackRoundtrip(const std::string &str) {
return ParseAndRegenerateString(str) == str;
}
TEST(VpiValue, ParseValueFindPrefix) {
EXPECT_EQ(ParseAndRegenerateString("INT:42"), "INT:42");
// With Whitespace in front.
EXPECT_EQ(ParseAndRegenerateString(" INT:42"), "INT:42");
// .. or with whitespace at end.
EXPECT_EQ(ParseAndRegenerateString("INT:42 "), "INT:42");
// .. or at both ends.
EXPECT_EQ(ParseAndRegenerateString(" INT:42 "), "INT:42");
}
TEST(VpiValue, ParseScalarValue) {
// Zero and one are represented as integers
EXPECT_EQ(ParseAndRegenerateString("SCAL:0"), "SCAL:0");
EXPECT_EQ(ParseAndRegenerateString("SCAL:1"), "SCAL:1");
// Symbolic scalar values
EXPECT_EQ(ParseAndRegenerateString("SCAL:Z"), "SCAL:Z");
EXPECT_EQ(ParseAndRegenerateString("SCAL:X"), "SCAL:X");
EXPECT_EQ(ParseAndRegenerateString("SCAL:H"), "SCAL:H");
EXPECT_EQ(ParseAndRegenerateString("SCAL:L"), "SCAL:L");
EXPECT_EQ(ParseAndRegenerateString("SCAL:W"), "SCAL:DontCare");
// The longer symbols are case-insensitive
EXPECT_EQ(ParseAndRegenerateString("SCAL:DontCare"), "SCAL:DontCare");
EXPECT_EQ(ParseAndRegenerateString("SCAL:dontcare"), "SCAL:DontCare");
EXPECT_EQ(ParseAndRegenerateString("SCAL:NoChange"), "SCAL:NoChange");
EXPECT_EQ(ParseAndRegenerateString("SCAL:nochange"), "SCAL:NoChange");
// Also parse numeric values
EXPECT_EQ(ParseAndRegenerateString("SCAL:2"), "SCAL:Z");
EXPECT_EQ(ParseAndRegenerateString("SCAL:3"), "SCAL:X");
EXPECT_EQ(ParseAndRegenerateString("SCAL:4"), "SCAL:H");
EXPECT_EQ(ParseAndRegenerateString("SCAL:5"), "SCAL:L");
EXPECT_EQ(ParseAndRegenerateString("SCAL:6"), "SCAL:DontCare");
EXPECT_EQ(ParseAndRegenerateString("SCAL:7"), "SCAL:NoChange");
// (Q: What is the difference between X and DontCare ?)
EXPECT_EQ(ParseAndRegenerateString("SCAL:6"), "SCAL:DontCare");
EXPECT_EQ(ParseAndRegenerateString("SCAL:7"), "SCAL:NoChange");
}
// Some smoke testing to see if a string we parse and regenerated is the same
TEST(VpiValue, roundtrip) {
EXPECT_TRUE(ParseConvertBackRoundtrip("INT:42"));
EXPECT_TRUE(ParseConvertBackRoundtrip("SCAL:1"));
EXPECT_TRUE(ParseConvertBackRoundtrip("SCAL:X"));
EXPECT_TRUE(ParseConvertBackRoundtrip("SCAL:Z"));
EXPECT_TRUE(ParseConvertBackRoundtrip("SCAL:H"));
EXPECT_TRUE(ParseConvertBackRoundtrip("SCAL:L"));
EXPECT_TRUE(ParseConvertBackRoundtrip("STRING:hello"));
EXPECT_TRUE(ParseConvertBackRoundtrip("HEX:AFFE"));
EXPECT_TRUE(ParseConvertBackRoundtrip("OCT:0123"));
EXPECT_TRUE(ParseConvertBackRoundtrip("BIN:11111"));
EXPECT_TRUE(ParseConvertBackRoundtrip("REAL:3.141592"));
}