sql connect
sleep 2000

sql show databases
sql drop database $data00 -x e1
e1:
sql show databases
sql drop database $data00 -x e2
e2:

$i = 0
$dbPrefix = tb_1f_db
$tbPrefix = tb_1f_tb
$db = $dbPrefix . $i
$tb = $tbPrefix . $i

print =============== step1
sql drop database -x step1
step1:
sql create database $db
sql use $db
sql create table $tb (ts timestamp, speed int)

#commit to file will trigger if insert 82 rows 

$N = 82 

print =============== step 1
$x = $N
$y = $N / 2
while $x > $y
  $ms = $x . m
  $xt = - . $x 
  sql insert into $tb values (now - $ms , $x ) 
  $x = $x - 1
endw 

sql select * from $tb
print sql select * from $tb -> $rows points 
if $rows != $y then 
  return -1
endi

$x = $N / 2
$y = $N
while $x < $y
  $ms = $x . m 
  sql insert into $tb values (now + $ms , $x ) 
  $x = $x + 1
endw 
sql select * from $tb
print sql select * from $tb -> $rows points 
if $rows != $N then 
  return -1
endi

print =============== step 2

$R = 4
$x = $N * 2
$y = $N * $R 
$expect = $y + $N
$y = $y + $x
while $x < $y
  $ms = $x . m 
  sql insert into $tb values (now + $ms , $x ) 
  $x = $x + 1
endw 

sql select * from $tb
print sql select * from $tb -> $rows points 
if $rows != $expect then 
  return -1
endi

print =============== step 3

$N1 = $N + 1
$result1 = $N / 2
$result2 = $N
$step = $N1 . m

$start1 = now- . $step
$start2 = now
$start3 = now+ . $step
$end1 = now- . $step
$end2 = now
$end3 = now+ . $step


sql select * from $tb where ts < $start1 and ts > $end1
if $rows != 0 then
  return -1
endi

sql select * from $tb where ts < $start1 and ts > $end2
if $rows != 0 then
  return -1
endi

sql select * from $tb where ts < $start1 and ts > $end3
if $rows != 0 then
  return -1
endi

sql select * from $tb where ts < $start2 and ts > $end1
print select * from $tb where ts < $start2 and ts > $end1 ->  $rows points
if $rows != $result1 then 
  return -1
endi

sql select * from $tb where ts < $start2 and ts > $end2
if $rows != 0 then
  return -1
endi

sql select * from $tb where ts < $start2 and ts > $end3
if $rows != 0 then
  return -1
endi

sql select * from $tb where ts < $start3 and ts > $end1
print sql select * from $tb where ts < $start3 and ts > $end1 -> $rows points 
if $rows != $result2 then 
  return -1
endi

sql select * from $tb where ts < $start3 and ts > $end2
print sql select * from $tb where ts < $start3 and ts > $end2 -> $rows points 
if $rows != $result1 then 
  return -1
endi

sql select * from $tb where ts < $start3 and ts > $end3 
if $rows != 0 then
  return -1
endi

print ================= order by ts desc  

sql select * from $tb where ts < $start1 and ts > $end1 order by ts desc
if $rows != 0 then
  return -1
endi

sql select * from $tb where ts < $start1 and ts > $end2 order by ts desc
if $rows != 0 then
  return -1
endi

sql select * from $tb where ts < $start1 and ts > $end3 order by ts desc
if $rows != 0 then
  return -1
endi

sql select * from $tb where ts < $start2 and ts > $end1 order by ts desc  
print select * from $tb where ts < $start2 and ts > $end1  order by ts desc  ->  $rows points
if $rows != $result1 then 
  return -1
endi

sql select * from $tb where ts < $start2 and ts > $end2  order by ts desc
if $rows != 0 then
  return -1
endi

sql select * from $tb where ts < $start2 and ts > $end3 order by ts desc
if $rows != 0 then
  return -1
endi

sql select * from $tb where ts < $start3 and ts > $end1 order by ts desc  
print sql select * from $tb where ts < $start3 and ts > $end1  order by ts desc  -> $rows points 
if $rows != $result2 then 
  return -1
endi

sql select * from $tb where ts < $start3 and ts > $end2 order by ts desc  
print sql select * from $tb where ts < $start3 and ts > $end2  order by ts desc  -> $rows points 
if $rows != $result1 then 
  return -1
endi

sql select * from $tb where ts < $start3 and ts > $end3   order by ts desc
if $rows != 0 then
  return -1
endi

clear:

sql drop database $db
sql show databases
if $rows != 0 then 
  return -1
endi
