1 -- test script for various Lua functions
2 -- use with dhcp.pcap in test/captures directory
4 local testlib
= require("testlib")
7 local PER_FRAME
= "per-frame"
10 -- expected number of runs per type
14 [PER_FRAME
]=n_frames
*5,
17 testlib
.init(taptests
)
20 -- the following are so we can use pcall (which needs a function to call)
21 local function setNSTime(nst
,name
,value
)
25 local function getNSTime(nst
,name
)
29 ------------- test script ------------
30 testlib
.testing(OTHER
,"negative tests")
31 testlib
.test(OTHER
,"NSTime.new-1",not pcall(NSTime
,"FooBARhowdy"))
32 testlib
.test(OTHER
,"NSTime.new-2",not pcall(NSTime
,"ip","FooBARhowdy"))
33 local tmptime
= NSTime()
34 testlib
.test(OTHER
,"NSTime.set-3",pcall(setNSTime
,tmptime
,"secs",10))
35 testlib
.test(OTHER
,"NSTime.set-4",not pcall(setNSTime
,tmptime
,"foobar",1000))
36 testlib
.test(OTHER
,"NSTime.set-5",pcall(setNSTime
,tmptime
,"nsecs",123))
37 testlib
.test(OTHER
,"NSTime.set-6",not pcall(setNSTime
,NSTime
,"secs",0))
38 testlib
.test(OTHER
,"NSTime.set-7",not pcall(setNSTime
,tmptime
,"secs","foobar"))
39 testlib
.test(OTHER
,"NSTime.set-8",not pcall(setNSTime
,NSTime
,"nsecs",0))
40 testlib
.test(OTHER
,"NSTime.set-9",not pcall(setNSTime
,tmptime
,"nsecs","foobar"))
42 testlib
.test(OTHER
,"NSTime.get-10",pcall(getNSTime
,tmptime
,"secs"))
43 testlib
.test(OTHER
,"NSTime.get-11",pcall(getNSTime
,tmptime
,"nsecs"))
44 testlib
.test(OTHER
,"NSTime.get-12",not pcall(getNSTime
,NSTime
,"secs"))
45 testlib
.test(OTHER
,"NSTime.get-13",not pcall(getNSTime
,NSTime
,"nsecs"))
48 testlib
.testing(OTHER
,"basic tests")
49 local first
= NSTime()
50 local second
= NSTime(100,100)
51 local third
= NSTime(0,100)
52 testlib
.test(OTHER
,"NSTime.secs-14", first
.secs
== 0)
53 testlib
.test(OTHER
,"NSTime.secs-15", second
.secs
== 100)
54 testlib
.test(OTHER
,"NSTime.secs-16", third
.secs
== 0)
56 testlib
.test(OTHER
,"NSTime.nsecs-17", first
.nsecs
== 0)
57 testlib
.test(OTHER
,"NSTime.nsecs-18", second
.nsecs
== 100)
58 testlib
.test(OTHER
,"NSTime.nsecs-19", third
.nsecs
== 100)
60 testlib
.test(OTHER
,"NSTime.eq-20", first
== NSTime())
61 testlib
.test(OTHER
,"NSTime.neq-21", second
~= third
)
63 testlib
.test(OTHER
,"NSTime.add-22", first
+ second
== second
)
64 testlib
.test(OTHER
,"NSTime.add-23", third
+ NSTime(100,0) == second
)
65 testlib
.test(OTHER
,"NSTime.add-24", NSTime(100) + NSTime(nil,100) == second
)
67 testlib
.test(OTHER
,"NSTime.lt-25", third
< second
)
68 testlib
.test(OTHER
,"NSTime.gt-26", third
> first
)
69 testlib
.test(OTHER
,"NSTime.le-27", second
<= NSTime(100,100))
71 testlib
.test(OTHER
,"NSTime.unm-28", -first
== first
)
72 testlib
.test(OTHER
,"NSTime.unm-29", -(-second
) == second
)
73 testlib
.test(OTHER
,"NSTime.unm-30", -second
== NSTime(-100,-100))
74 testlib
.test(OTHER
,"NSTime.unm-31", -third
== NSTime(0,-100))
76 testlib
.test(OTHER
,"NSTime.tostring-32", tostring(first
) == "0.000000000")
77 testlib
.test(OTHER
,"NSTime.tostring-33", tostring(second
) == "100.000000100")
78 testlib
.test(OTHER
,"NSTime.tostring-34", tostring(third
) == "0.000000100")
80 testlib
.test(OTHER
,"NSTime.tonumber-35", first
:tonumber() == 0.0)
81 testlib
.test(OTHER
,"NSTime.tonumber-36", second
:tonumber() == 100.0000001)
82 testlib
.test(OTHER
,"NSTime.tonumber-37", third
:tonumber() == 0.0000001)
84 testlib
.testing(OTHER
,"setters/getters")
87 testlib
.test(OTHER
,"NSTime.set-38", first
== NSTime(123,100))
88 testlib
.test(OTHER
,"NSTime.get-39", first
.secs
== 123)
89 testlib
.test(OTHER
,"NSTime.get-40", first
.nsecs
== 100)
91 local minus0_4
= NSTime() - NSTime(0,400000000)
92 testlib
.test(OTHER
,"NSTime.negative_tonumber-41", minus0_4
:tonumber() == -0.4)
93 testlib
.test(OTHER
,"NSTime.negative_tostring-42", tostring(minus0_4
) == "-0.400000000")
94 local minus0_4
= NSTime() - NSTime(1,400000000)
95 testlib
.test(OTHER
,"NSTime.negative_tonumber-43", minus0_4
:tonumber() == -1.4)
96 testlib
.test(OTHER
,"NSTime.negative_tostring-44", tostring(minus0_4
) == "-1.400000000")
99 ----------------------------------
101 -- declare some field extractors
102 local f_frame_time
= Field
.new("frame.time")
103 local f_frame_time_rel
= Field
.new("frame.time_relative")
104 local f_frame_time_delta
= Field
.new("frame.time_delta")
106 local tap
= Listener
.new()
108 local begin
= NSTime()
111 function tap
.packet(pinfo
,tvb
,frame
)
112 testlib
.countPacket(FRAME
)
113 testlib
.testing(FRAME
,"NSTime in Frame")
115 local fi_now
= f_frame_time()
116 local fi_rel
= f_frame_time_rel()
117 local fi_delta
= f_frame_time_delta()
119 testlib
.test(PER_FRAME
,"typeof-1", typeof(begin
) == "NSTime")
120 testlib
.test(PER_FRAME
,"typeof-2", typeof(fi_now()) == "NSTime")
123 if testlib
.getPktCount(FRAME
) == 1 then
124 testlib
.test(PER_FRAME
,"__eq-1", begin
== fi_delta())
125 testlib
.test(PER_FRAME
,"NSTime.secs-1", fi_delta().secs
== 0)
126 testlib
.test(PER_FRAME
,"NSTime.nsecs-1", fi_delta().nsecs
== 0)
129 testlib
.test(PER_FRAME
,"__sub__eq-1", now
- previous
== fi_delta())
130 testlib
.test(PER_FRAME
,"__sub__eq-2", now
- begin
== fi_rel())
131 testlib
.test(PER_FRAME
,"__add-1", (previous
- begin
) + (now
- previous
) == fi_rel())