sbtrees_test.anubis 3.07 KB

                                The Anubis Project 
                                A test for sbtrees. 


read tools/basis.anubis
read tools/sbtrees.anubis


define SBTree(String,String)
   com_put
   ( 
     SBTree(String,String)  t
   ) =
   with key = prompt("key: "), 
      value = prompt("Value: "), 
      put(key,value,t). 

define SBTree(String,String)
   com_rput1
   ( 
     String                 key,
     SBTree(String,String)  t
   ) = 
   with   value = random(100000),  
     put(key,to_hexa(extract(sha1(value),0,3)),t). 

define SBTree(String,String)
   put_several
   ( 
     String                 prefix, 
     Int                    n, 
     SBTree(String,String)  t
   ) =       
   if n < 1 then t else
   with t1 = com_rput1(prefix+n,t),
   put_several(prefix,n-1,t1). 
      
      
define SBTree(String,String)
   com_rput
   ( 
     SBTree(String,String)  t
   ) =       
   with sn = prompt("Number of entries: "),
       pre = prompt("key prefix: "), 
   if decimal_scan(sn) is 
   {
     failure     then print("Incorrect number.\n"); t, 
     success(n)  then if n < 0
                      then print("Enter a positive number, please.\n"); com_rput(t)
                      else put_several(pre,n,t)
   }.
   
define SBTree(String,String)
   com_unput
   ( 
     SBTree(String,String)  t
   ) =
   with key = prompt("Key: "), 
     unput(key,t). 
     
define SBTree(String,String)
   com_get
   ( 
     SBTree(String,String)  t
   ) =
   with key = prompt("Key: "), 
     if get_value(key,t) is 
     {
       failure       then print("No value for key: "+key+"\n"),
       success(val)  then print(key+" = "+val+"\n")
     }; t. 
   
     
define One 
   show
   ( 
     SBTreeC(String,String)  t,
     String                  indent
   ) =
   if t is 
   {
     entry(k,v)  then 
       print(indent+((Word32)1)+"           "+k+" : "+v+"\n"), 
     
     node(ne,fk,lk,lb,rb)  then 
       print(indent+ne+"\n"); show(lb,indent+"    "); 
                              show(rb,indent+"    ")
   }. 
     
define SBTree(String,String)
   com_show
   ( 
     SBTree(String,String)  t
   ) =
   if t is sbtree(_,mbe_tree) then 
   if mbe_tree is 
   {
     empty       then print("B-tree is empty.\n"), 
     full(tree)  then show(tree,"")
   }; t. 
     
      

define One
   loop
   (
     SBTree(String,String)  t
   ) =
   if is_balanced(t) then 
   with com = prompt("\nEnter one character among these:\n   "+
                     " p (put), r (random put), u (unput), g (get), s (show), q (quit)\n"), 
   if com = "p" then loop(com_put(t))   else
   if com = "r" then loop(com_rput(t))  else
   if com = "u" then loop(com_unput(t)) else
   if com = "g" then loop(com_get(t))   else
   if com = "s" then loop(com_show(t))  else
   if com = "q" then unique             else
   print("Error: "+com+"\n"); loop(t)
   else print("Tree not balanced.\n"). 

define SBTComp
  compare
  (
    String   x, 
    String   y
  ) =
  if x = y then same else
  if x < y then before else after. 

global define One
  sbtrees_test
  (
    List(String) args
  ) =
  with t = new_sbtree("",compare), 
  loop(t).