moo/kernel/test-002.moo

227 lines
4.8 KiB
Smalltalk
Raw Permalink Normal View History

2016-03-28 13:27:03 +00:00
#include "Moo.moo".
2016-03-28 13:27:03 +00:00
////////////////////////////////////////////////////////////////#
// MAIN
////////////////////////////////////////////////////////////////#
2016-03-28 13:27:03 +00:00
2017-06-07 17:22:14 +00:00
class MyObject(Object)
2016-03-28 13:27:03 +00:00
{
2017-06-07 17:22:14 +00:00
var(#class) a := 100.
var(#class) ensure_tester_v := 0.
method(#class) test_quicksort: anArray
{
| quicksort |
quicksort := nil.
quicksort := [ :data :from :to |
(from < to)
ifTrue: [ | pivot index |
pivot := data at: from.
data swap: from with: to.
index := from.
from to: (to - 1) do: [ :each |
(data at: each) < pivot
ifTrue: [
data swap: each with: index.
index := index + 1
]
].
data swap: to with: index.
quicksort value: data value: from value: index - 1.
quicksort value: data value: index + 1 value: to
].
data
].
^(quicksort value: anArray value: 0 value: (anArray size - 1))
}
method(#class) test_on_do_with: dividend with: divisor
{
// it returns 0 upon an exception otherwise, division result.
^[ dividend div: divisor ] on: Exception do: [:ex | 0 ]
}
method(#class) test_ensure_with: v
{
[
[ Exception signal: "test ensure exception" ] ensure: [ self.ensure_tester_v := v ].
] on: Exception do: [:ex | /* do nothing */ ].
}
method(#class) test_ensure2_with: v
{
[ ^v * v ] ensure: [ self.ensure_tester_v := v ].
}
2016-03-28 13:27:03 +00:00
2017-06-07 17:22:14 +00:00
method(#class) proc1
2016-03-28 13:27:03 +00:00
{
[ System sleepForSecs: 1. a := a + 100 ] newProcess resume.
2017-06-07 17:22:14 +00:00
^a
2016-03-28 13:27:03 +00:00
}
2017-07-21 16:54:43 +00:00
method(#class) test_semaphore_heap
{
| sempq a |
sempq := SemaphoreHeap new.
'--------------------------' dump.
1 to: 200 by: 1 do: [ :i |
| sem |
sem := Semaphore new.
sem fireTime: (200 - i).
sempq insert: sem
].
'--------------------------' dump.
2019-09-14 02:21:59 +00:00
sempq removeAt: 40.
sempq removeAt: 50.
sempq removeAt: 100.
2017-07-21 16:54:43 +00:00
a := -100.
[sempq size > 0] whileTrue: [
| sem b |
sem := sempq popTop.
b := sem fireTime.
if (a > b) { ^false }.
a := b.
].
^true
}
method(#class) test_mutex
{
| mtx sem v p q |
mtx := Mutex new.
sem := Semaphore new.
p := 0.
[ mtx lock.
v := 0.
2000 timesRepeat: [v := v + 1. p := p + 1. ].
q := v.
mtx unlock.
sem signal.
] fork.
[ mtx critical: [
v := 0.
2000 timesRepeat: [v := v + 1. p := p + 1. ].
q := v.
].
sem signal.
] fork.
mtx lock.
v := 0.
2000 timesRepeat: [v := v + 1. p := p + 1. ].
mtx unlock.
sem wait.
sem wait.
^##( v, p ) // v must be 2000, p must be 6000
}
/*
method(#class) test_sem_sig
{
| s |
s := Semaphore new.
s signalAction: [:sem | 'SIGNAL ACTION............' dump. ].
[ System sleepForSecs: 1. s signal ] fork.
s wait.
}
method(#class) test_semgrp
{
| sg |
sg := SemaphoreGroup new.
sg add: s1 withAction: [].
sg add: s2 withAction: [].
sg add: s3 withAction: [].
sg wait.
}
*/
2017-06-07 17:22:14 +00:00
method(#class) main
2016-03-28 13:27:03 +00:00
{
2017-06-07 17:22:14 +00:00
| tc limit |
2016-03-28 13:27:03 +00:00
tc := ##(
// 0 - 4
[ (self test_quicksort: #(7 12 3 20 5 8 2) copy) = #(2 3 5 7 8 12 20)],
[ (self test_quicksort: ##(99, 12, 18, 7, 12, 3, 20, 5, 8, 2)) = #(2 3 5 7 8 12 12 18 20 99)],
[ (self test_on_do_with: 10 with: 2) == 5 ],
[ (self test_on_do_with: -10 with: 0) == 0 ],
[ self test_ensure_with: -20945. self.ensure_tester_v == -20945 ],
// 5-9
[ ((self test_ensure2_with: 8) == 64) and: [self.ensure_tester_v == 8] ],
2017-06-07 17:22:14 +00:00
[ self proc1 == 100 ],
[ System sleepForSecs: 2. self proc1 == 200 ],
[ self test_semaphore_heap == true ],
[ self test_mutex = #(2000 6000) ],
// 10-14
////[ self test_sem_sig ],
[ System sleepForSecs: 2. self.a == 300 ] // sleep before checking self.a to prevent different result depending on process switching frequency and speed
2017-06-16 09:45:22 +00:00
).
2016-03-28 13:27:03 +00:00
2017-06-07 17:22:14 +00:00
limit := tc size.
2016-03-28 13:27:03 +00:00
2017-06-07 17:22:14 +00:00
0 priorTo: limit by: 1 do: [ :idx |
| tb |
tb := tc at: idx.
System log(System.Log.INFO, idx asString, (if (tb value) { " PASS" } else { " FAIL" }), "\n").
2017-07-21 16:54:43 +00:00
].
// [ |k| k := B"ABC\xFF". k at: 3 put: 16r65. k dump ] value.
// [ |k| k := "ABC\xFF". k at: 3 put: $X. k dump ] value.
//2 timesRepeat: [ |k| k := "ABC\xFF" copy. k dump. k at: 3 put: $X. k dump ].
//2 timesRepeat: [ |k| k := B"ABC\xFF" copy. k dump. k at: 3 put: 16r39. k dump ].
//B"" dump.
//##(#( 1 2 3 4 5 6 7 8 9 ) asOrderedCollection select: [:x | x > 5]) do: [:x | x dump ].
//(#( 1 2 3 4 5 6 7 8 9 ) select: [:x | x > 5]) dump.
//(#( 1 2 3 4 5 6 7 8 9 ) collect: [:x | x * 2]) dump.
//(#( 1 2 3 4 5 6 7 8 9 ) reject: [:x | x > 5]) dump.
2016-03-28 13:27:03 +00:00
}
}
/*
s1 := TcpSocket new.
s1 onEvent: #connected do: [
2017-10-30 01:11:18 +00:00
s1 waitToRead.
//s1 beginWrite: C'GET / HTTP/1.0\n\r'.
]
s1 onEvent: #written do: [
2017-10-30 01:11:18 +00:00
].
2017-10-30 01:11:18 +00:00
s1 onEvent: #readyToRead do: [
].
2017-10-30 01:11:18 +00:00
s1 beginConnect: '1.2.3.4:45' onConnected: [ :result | xxxx].
////
2017-10-30 01:11:18 +00:00
s1 beginConnect: destination onConnected:
s1 endConnect --> return what?
s1 endReceive
s1 beginReceive: buffer callback: [xxxx].
s1 beginSend: data onEnd: [do this].
s1 endSend
s1 beginAccept: [callback]
s1 endAccept -> returns the actual socket
*/