-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcl_functions.inc
143 lines (125 loc) · 4.52 KB
/
tcl_functions.inc
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
###############################################################
# Heart of expect-lite crg (constrained random generator) lite
# functions. The functions are called from expect-lite
#
# set_random_seed <seed> sets the random seed
# random <start> <end> returns random number in range start-end
# randomize <var> <start> <end>
# sets expect-lite variable to random value in range start-end
#
# Include file sets seed, and initialized Random Number Generator
################################################################
#
# Additonal functions in this file
#
# dec2hex <hex_var_name> converts decimal a hexadecimal
# incr_wrap <var> <start> <end>
# increments expect-lite var unless at $end,
# then wraps to $start
#
# show_all_vars <filter_str>
# Shows all defined expect-lite Vars using 'str' as filter
# use "*" to show all defined variables
#
# mark_time <var_name>
# grabs current time (with milisecond resolution) and places value in var_name
#
# show_time <var_time_ms> <var_name>
# var_time_ms is time in miliseconds (from the epoch) returned by mark_time
# returns date/time formatted string in var_name
#
################################################################
# Updated for expect-lite 4.10 namespace - 8/18/11
# Updated with time functions (version 4.7.0) - 3/30/14
########## Include file which 'sources' tcl functions
# Define random functions
!proc set_random_seed { var } {
# proc sets the random seed based on expect-lite variable $var
! set seed $::expectlite::user_namespace($var)
! if { $seed == "none" } {
! set seed [clock seconds]
! expr srand($seed)
! set ::expectlite::user_namespace($var) $seed
! } else {
! expr srand($seed)
! }
! puts "Seed is:$seed"
!}
!proc random { start end } {
# proc returns random number in range start-end
! set range [ expr $end +1 - $start ]
! return [expr int(rand()*$range) + $start]
!}
!proc randomize { var start end } {
# proc sets expect-lite variable to random value in range start-end
! set ::expectlite::user_namespace($var) [random $start $end]
!}
>
#
# Define Additional Functions
#
!proc dec2hex { hex_var_name } {
! # A Procedure that converts decimal a hexadecimal value, updates expect-lite variable
! set x $::expectlite::user_namespace($hex_var_name)
! set x [format %02X $x]
! set ::expectlite::user_namespace($hex_var_name) $x
!}
!proc bitwise { var oper val } {
# proc sets expect-lite variable to bitwise operation 'oper' by $val
# supports oper: << >> & | ^ + = * / %
# depricated by expect-lite math function
! set ::expectlite::user_namespace($var) [expr $::expectlite::user_namespace($var) $oper $val]
!}
>
!proc incr_wrap { var start end } {
# increments expect-lite var unless at $end, then wraps to $start
! if { $::expectlite::user_namespace($var) == [expr $end ] } {
! set ::expectlite::user_namespace($var) $start
! } else {
! incr ::expectlite::user_namespace($var)
! }
!}
>
# Shows all defined expect-lite Vars using 'str' as filter
!proc show_all_vars { str } {
! set var_list [array names ::expectlite::user_namespace]
! set var_list_sorted [lsort $var_list]
! puts "Displaying expect-lite Variables:"
! foreach i $var_list_sorted {
! if { $str != "*" } {
! if { [ regexp ".*$str.*" $i ] } {
! puts "Var:$i \t\t\Value:$::expectlite::user_namespace($i)"
! }
! } else {
! puts "Var:$i \t\t\Value:$::expectlite::user_namespace($i)"
! }
! }
!}
>
# Time function: mark time, place into expect-lite variable (reference by name)
!proc mark_time { var_name } {
! # A Procedure that marks current time in milliseconds and, updates expect-lite variable
! set time [clock clicks -milliseconds ]
! set ::expectlite::user_namespace($var_name) $time
!}
# Time function: converts time format (in miliseconds) and places into expect-lite variable (reference by name)
!proc show_time { var_time_ms var_name} {
! # A Procedure that converts time from milliseconds and, updates expect-lite variable
! set date $::expectlite::user_namespace($var_time_ms)
! set ms [expr $date % 1000]
! set date [expr int($date / 1000)]
! set date_str [clock format $date -format "%Y-%m-%d %H:%M:%S"]
! set date_str "$date_str.$ms"
! set ::expectlite::user_namespace($var_name) $date_str
!}
# set the default value of the random number gen (rng) seed to "none"
$seed=none
# allow constant to override variable
# Normally constants will over-ride variables, but
# because $seed is a user_namespace variable, and referenced that way in
# the procedure set_random_seed, we use a trick, to set $seed to the value
# of the constant $seed.
$seed=$seed
; === initialize the rng
!set_random_seed seed
#pau