test_CommonCodec.ttcn3 1.73 KB
Newer Older
filatov's avatar
filatov committed
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
module test_CommonCodec /*language "TTCN-3:2009 Advanced Parameterization"*/ {

	type component TCType { }

	type record TestRecord {
		bitstring bs optional
	}

	function test_decodePDU<PDUType>(in template PDUType p_pdu, in template (present) octetstring p_expEncMsg) runs on TCType {
		var PDUType v_decMsg;
		var bitstring v_encMsg;
		var integer v_res := 0;

		v_res := decvalue(oct2bit(p_expEncMsg), v_decMsg);
		select (v_res) {
			case (0) {
				if(match(v_decMsg, p_pdu)) {
					setverdict(pass);
				} else {
					setverdict(fail);
				}
			}
			case (1) {
				setverdict(fail, "Decoding failed.");
			}
			case (2) {
				setverdict(fail, "Not enough bits.");
			}
		} // End of 'select' statement
	} // End of function test_decodePDU<PDUType>

	function test_PDU<PDUType>(in PDUType p_pdu, in boolean p_decode:=true, in template (omit) bitstring p_expEncMsg:=omit) runs on TCType {
		var PDUType v_decMsg;
		var bitstring v_encMsg;
		var integer v_res := 0;
		var TestRecord v_tr := { bs := p_expEncMsg }
		
		v_encMsg := encvalue(p_pdu);
		log("Encoded message: ", v_encMsg);
		
		if (v_encMsg == null) {
			setverdict(fail, "Encoding failed!");
			stop;
		}
		
		if (ispresent(v_tr.bs)) {
			if (not match(v_encMsg, p_expEncMsg)) {
				setverdict(fail, "Encoding failed, not the expected result!");
				stop;
			}
		}
		
		setverdict(pass, "Encoding passed.");
		
		if (p_decode == true) {
			v_res := decvalue(v_encMsg, v_decMsg);
			select (v_res) {
				case (0) {
					if(match(v_decMsg, p_pdu)) {
						setverdict(pass);
					} else {
						setverdict(fail);
					}
				}
				case (1) {
					setverdict(fail, "Decoding failed.");
				}
				case (2) {
					setverdict(fail, "Not enough bits.");
				}
			}
		}
	} // End of function test_PDU<PDUType>
}