summaryrefslogtreecommitdiff
path: root/cesar/maximus/python/test/test_simu.py
blob: a7e9958be417d39aed9bbea85876fd58ad0af916 (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
#! usr/bin/env python

import sys, startup

from maximus.simu import *
from maximus.simu.rx import *
from interface import *


# SIMU TEST

# Create a Maximus instance
m = Maximus()

# Initialize Maximus with command line arguments
m.init(sys.argv)

# Transmission
rsp = sendnrecv(file='data', timeout=5000)
send(file='data')

# Define a filter function
def my_filter(rx_macframe):
	print "=> my_filter"
	return True

# Reception
rsp = recv(maximus=m, timeout=10000, filter=my_filter, count=0)

# Get the current date
t = m.get_date()


# DOC TEST

import doctest
doctest.testmod(rx)
doctest.testmod(tx)


# UNIT TEST

import unittest

class TestRxFunctions(unittest.TestCase):

	def setUp(self):
		print "setUp"
		self.reception = Rx(m)

	def tearDown(self):
		print "tearDown"

	def test_init(self):
		 reception = Rx(maximus=m, filter_fc=my_filter)

	def test_recv1(self):
		reception = Rx(maximus=m, filter_fc=my_filter, counter=2)
		class MACFrame:
			pass
		macframe1 = MACFrame()
		macframe2 = MACFrame()
		cb(reception, macframe1)
		cb(reception, macframe2)
		self.assertEqual(reception.recv(), [macframe1, macframe2])
		self.assertEqual(reception.get_filter_fc(), None)

	def test_add_frame(self):
		class MACFrame:
			pass
		macframe1 = MACFrame()
		macframe2 = MACFrame()
		self.reception.add_frame(macframe1)
		self.assertEqual(len(self.reception.get_frame_list()), 1)
		self.assertEqual(self.reception.get_frame_list()[0], macframe1)
		self.reception.add_frame(macframe2)
		self.assertEqual(len(self.reception.get_frame_list()), 2)
		self.assertEqual(self.reception.get_frame_list()[0], macframe1)
		self.assertEqual(self.reception.get_frame_list()[1], macframe2)

	def test_set_counter(self):
		counter = 10
		self.reception.set_counter(counter)
		self.assertEqual(self.reception.get_counter(), counter)

	def test_decr_counter(self):
		self.reception.decr_counter()
		self.assertEqual(self.reception.get_counter(), 0)
		counter = 10
		self.reception.set_counter(counter)
		self.reception.decr_counter()
		self.assertEqual(self.reception.get_counter(), counter-1)

	def test_call_filter_fc(self):
		def filter_fc(macframe):
			print "=> filter_fc"
		self.reception.set_filter_fc(filter_fc)
		class MACFrame:
			pass
		macframe = MACFrame()
		self.reception.call_filter_fc(macframe)

	def test_recv2(self):
		self.assertEqual(recv(maximus=m, timeout=None, filter=None, count=0), None)
		t = m.get_date()
		d = 750001
		recv(maximus=m, timeout=d)
		self.assertEqual(m.get_date(), t + 1000000)

suite = unittest.TestLoader().loadTestsFromTestCase(TestRxFunctions)

try:
	suite.addTest(doctest.DocTestSuite(rx))
	suite.addTest(doctest.DocTestSuite(tx))
except ValueError:
	print "has no tests"

if __name__ == '__main__':
	testResult = unittest.TextTestRunner(verbosity=2).run(suite)
	# For nightly build errors
	sys.exit ((1, 0)[testResult.wasSuccessful ()])