
list tests
 test1
 test2
 test3
 memtest1
end

# test using even samples
macro test1
 $PASS = 1
 break -auto off

 local P PI
 $PI = 3.14159265359
 $P  = 15.0

 delete -q t f period power

 create t 0 100
 set f = sin(2*$PI*t/$P)
 periodogram t f 5 50 period power

 peak -q period power

 if (abs ($peakpos - $P) > 0.5)
   $PASS = 0
   echo "OFFSET: {$peakpos - $P}"
 end
end

# test using random samples
macro test2
 $PASS = 1
 break -auto off

 local P PI
 $PI = 3.14159265359
 $P  = 15.0

 delete -q x t f period power

 create x 0 100
 set t = 100 * rnd(x) 
 set f = sin(2*$PI*t/$P)
 periodogram t f 5 50 period power

 # lim -n 0 t f; clear; box; plot -x 2 -pt 2 t f
 # lim -n 1 period power; clear; box; plot period power

 peak -q period power

 if (abs ($peakpos - $P) > 0.5)
   $PASS = 0
 end
end

# test using random samples, higher frequency
macro test3
 $PASS = 1
 break -auto off

 local P PI
 $PI = 3.14159265359
 $P  = 2.0

 delete -q x t f period power

 create x 0 100
 set t = 100 * rnd(x) 
 set f = sin(2*$PI*t/$P)

 periodogram t f 1 10 period power

 # lim -n 0 t f; clear; box; plot -x 2 -pt 2 t f
 # lim -n 1 period power; clear; box; plot period power

 peak -q period power

 if (abs ($peakpos - $P) > 0.05)
   $PASS = 0
 end
end

# test using random samples, offset start
macro test4
 $PASS = 1
 break -auto off

 local P PI
 $PI = 3.14159265359
 $P  = 15.0

 delete -q x t f period power

 create x 500 800
 set t = 300 * rnd(x) + 500
 set f = sin(2*$PI*t/$P)

 periodogram t f 2 30 period power

#  lim -n 0 t f; clear; box; plot -x 2 -pt 2 t f
#  lim -n 1 period power; clear; box; plot period power

 peak -q period power

 if (abs ($peakpos - $P) > 0.05)
   $PASS = 0
 end
end

# Memory test
macro memtest1

 local i
 local P PI
 $PI = 3.14159265359
 $P  = 15.0

 delete -q x t f period power

 create x 500 800
 set t = 300 * rnd(x) + 500
 set f = sin(2*$PI*t/$P)

 list word -x "ps -p $PID -o rss"
 $startmem = $word:1

 for i 0 100
  periodogram t f 2 30 period power
 end
  
 list word -x "ps -p $PID -o rss"
 $endmem = $word:1

 $PASS = 1

 if ($endmem - $startmem > 180)
   $PASS = 0
   echo "growth: {$endmem-$startmem}"
   echo "kB/loop: {($endmem-$startmem)/100}"
 end
end
