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
|
# This test suite is for validating scheduler simultaneous event ordering in ns
#
# To run all tests: test-all-scheduler
#
# To run individual tests:
# ns test-suite-scheduler.tcl List
# ns test-suite-scheduler.tcl Calendar
# ns test-suite scheduler.tcl Heap
#
# To view a list of available tests to run with this script:
# ns test-suite-scheduler.tcl
#
remove-all-packet-headers ; # removes all except common
add-packet-header Flags IP TCP ; # hdrs reqd for validation test
# FOR UPDATING GLOBAL DEFAULTS:
# What does this simple test do?
# - it schedules $TIMES batches of events. Each batch contains $SIMUL events,
# all of which will occur at the same time. All events are permuted and
# scheduled in a random order. The output should be a list of integers
# from 1 to ($TIMES*$SIMUL) in increasing order.
# - if the output differs it exits with status 1, otherwise it exits with status 0.
proc comp {a b} {
set a1 [lindex $a 0]
set b1 [lindex $b 0]
if {$a1 > $b1} {
return 1
}
return 0
}
Class TestSuite
TestSuite instproc init { quiet } {
$self instvar ns_ rng_ N_ quiet_
set ns_ [new Simulator]
set rng_ [new RNG]
set N_ 0
set quiet_ $quiet
}
TestSuite instproc run { scheduler } {
$self instvar ns_ rng_ N_
if { [catch "$ns_ use-scheduler $scheduler"] } {
puts "*** WARNING: scheduler Scheduler/$scheduler is not supported, test was not run"
exit 0
}
set TIMES 20 ;# $TIMES different times for events
set SIMUL 50 ;# each occurs $SIMUL times
set TIMEMIN 0 ;# random events are taken from [TIMEMIN, TIMEMAX]
set TIMEMAX 5
# generate random event timings and put them in increasing order by time to occur
for {set i 0 } { $i < $TIMES } { incr i } {
lappend timings [list [$rng_ uniform $TIMEMIN $TIMEMAX] $i $SIMUL]
}
set stimings [lsort -command "comp" $timings]
for {set i 0 } { $i < $TIMES } { incr i } {
set e [lindex $stimings $i]
set idx [lsearch $timings $e]
set timings [lreplace $timings $idx $idx [lappend e $i]]
}
while 1 {
# pull out timings in random order
set i [expr int([$rng_ uniform 0 [llength $timings]])]
set e [lindex $timings $i]
set order [lindex $e 3]
set left [lindex $e 2]
set label [expr $SIMUL - $left + 1 + $order*$SIMUL]
$ns_ at [lindex $e 0] "$self assert $label"
incr left -1
if {$left==0} {
set timings [lreplace $timings $i $i]
if {$timings == ""} break
} else {
set e [lreplace $e 2 2 $left]
set timings [lreplace $timings $i $i $e]
}
}
$ns_ run
exit 0
}
TestSuite instproc assert { n } {
$self instvar N_ quiet_
if { $quiet_ != "QUIET" } {
puts $n
}
if [expr $n != $N_ + 1 ] {
exit 1
}
set N_ $n
}
TestSuite proc usage {} {
global argv0
puts stderr "usage: ns $argv0 <scheduler> \[quiet\]"
exit 1
}
global argc argv
set quiet ""
if { $argc == 2 } {
set quiet [lindex $argv 1]
if { $quiet != "QUIET" && $quiet != "quiet" } {
TestSuite usage
}
set quiet "QUIET"
}
if { $argc > 0 && $argc < 3 } {
set scheduler [lindex $argv 0]
} else {
TestSuite usage
}
set test [new TestSuite $quiet]
$test run $scheduler
|