Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / bench_netsim.nit
index 74781ca..6531814 100644 (file)
 # limitations under the License.
 
 class Node
-       redef meth to_s: String
+       redef fun to_s: String
        do
                return _name
        end
-       attr _name: String
+       var name: String = "noname"
 end
 
 class WakeUpNode
-special Node
-       meth wake_up is abstract
+       super Node
+       fun wake_up is abstract
        # Wake up the node
-       meth wake_up_in(d: Int)
+       fun wake_up_in(d: Int)
        # Ask to be weaked up in `d' time units
        do
                _scheduler.add_event(self, d)
        end
-       attr _scheduler: Scheduler
+       var scheduler: Scheduler is noinit
 end
 
 class NodeSource
-special Node
-       attr _nexts: ArraySet[NodeSink]
-       meth attach(n: NodeSink)
+       super Node
+       var nexts: nullable ArraySet[NodeSink] = null
+       fun attach(n: NodeSink)
        # Add the sink `n' the the connected nodes
        # Do nothing if `n' is already connected
        do
-               assert n != null
                # Create the collection if needed
                if _nexts == null then
                        _nexts = new ArraySet[NodeSink]
@@ -49,45 +48,44 @@ special Node
                _nexts.add(n)
        end
        
-       meth detach(n: NodeSink)
+       fun detach(n: NodeSink)
        # Remove the sink `n' from the connected nodes
        # Do nothing if `n' is not connected
        do
-               assert n != null
                _nexts.remove(n)
        end
 
-       protected meth send
+       protected fun send
        # Notify the sinks by calling the recieve(1) method of each sink
        do
                if _nexts == null then
                        return
                end
-               for n in _nexts do
+               for n in _nexts.as(not null) do
                        n.recieve(self)
                end
        end
 end
 
 class NodeSink
-special Node
-       meth recieve(n: NodeSource) is abstract
+       super Node
+       fun recieve(n: NodeSource) is abstract
        # the `n' has emeted a signal
 end
 
 #
 
 class Scheduler
-       attr _time_list: Array[Couple[Int, WakeUpNode]]
-       attr _time: Int # What time is it ?
-       meth add_event(n: WakeUpNode, d: Int)
+       var time_list: Array[Couple[Int, WakeUpNode]] = new Array[Couple[Int, WakeUpNode]]
+       var time: Int = 0 # What time is it ?
+       fun add_event(n: WakeUpNode, d: Int)
        # The node `n' whant to be weaked up in `d' time units
        do
                var entry = new Couple[Int, WakeUpNode](d+_time, n)
                _time_list.add(entry)
        end
 
-       meth next_event: WakeUpNode
+       fun next_event: nullable WakeUpNode
        # Get the
        do
                if _time_list.is_empty then
@@ -107,9 +105,9 @@ class Scheduler
                return entry.second
        end
 
-       meth run_for(time_limit: Int)
+       fun run_for(time_limit: Int)
        do
-               while true do
+               loop
                        var node = next_event 
                        if _time > time_limit then
                                print("Time limit.")
@@ -125,22 +123,21 @@ class Scheduler
 
        init
        do
-               _time_list = new Array[Couple[Int, WakeUpNode]]
        end
 end
 
 #
 
 class BeepSource
-special NodeSource
-special WakeUpNode
-       redef meth wake_up
+       super NodeSource
+       super WakeUpNode
+       redef fun wake_up
        do
                send
                wake_up_in(_delay)
        end
-       attr _delay: Int
-       meth start
+       var delay: Int
+       fun start
        do
                wake_up_in(_delay)
        end
@@ -154,16 +151,16 @@ special WakeUpNode
 end
 
 class CountSink
-special NodeSink
-       readable attr _count: Int
-       redef meth recieve(n: NodeSource)
+       super NodeSink
+       var count: Int = 0
+       redef fun recieve(n: NodeSource)
        do
-               _count = _count + 1
+               count = count + 1
        end
 end
 
 class SimpleCountSink
-special CountSink
+       super CountSink
 
        init(name: String)
        do
@@ -172,10 +169,10 @@ special CountSink
 end
 
 class NodeAlternate
-special NodeSink
-special NodeSource
-       attr _last: NodeSource
-       redef meth recieve(n: NodeSource)
+       super NodeSink
+       super NodeSource
+       var last: nullable NodeSource
+       redef fun recieve(n: NodeSource)
        do
                if n != _last then
                        _last = n
@@ -190,17 +187,17 @@ special NodeSource
 end
 
 class NodeEat
-special CountSink
-special NodeSource
-       attr _limit: Int
-       redef meth recieve(n: NodeSource)
+       super CountSink
+       super NodeSource
+       var limit: Int
+       redef fun recieve(n: NodeSource)
        do
-               var c = _count + 1
+               var c = count + 1
                if c >= _limit then
-                       _count = 0
+                       count = 0
                        send
                else
-                       _count = c
+                       count = c
                end
        end
 
@@ -212,15 +209,15 @@ special NodeSource
 end
 
 class NodeDelay
-special NodeSource
-special NodeSink
-special WakeUpNode
-       attr _delay: Int
-       redef meth recieve(n: NodeSource)
+       super NodeSource
+       super NodeSink
+       super WakeUpNode
+       var delay: Int
+       redef fun recieve(n: NodeSource)
        do
                wake_up_in(_delay)
        end
-       redef meth wake_up
+       redef fun wake_up
        do
                send
        end
@@ -270,11 +267,11 @@ e1.attach(a1)
 b1.start
 b2.start
 
-var nb = 100000
+var nb = 10
 if not args.is_empty then
        nb = args.first.to_i
 end
 
-s.run_for(nb)
+s.run_for(1 << nb)
 print(c1.count)