Examples and Tutorials


If you want to build and run the examples and tutorials see
Getting Started.


Working with an array, including insert/retrieve, iterate, copy,
compare:

use Container:Array;

class Test:TestArray {
   
   sub main() {
      return(testArray());
   }
   
   sub testArray() {
      var uux = new (Array, 5);
      
      uux.put(0, "Hi");
      
      if (uux.get(0) == "Hi") {
         " PASSED put, get".print();
      } else {
         "!FAILED put, get".print();
         return(false);
      }
      var x = 0;
      for (var i = uux.iterator;i.hasNext;;) {
         i.next;
         x = x++;
      }
      
      if (x == 5) {
         " PASSED iterate ".print();
      } else {
         "!FAILED iterate".print();
         return(false);
      }
      
      for (i = 0;i < uux.length;i = i++;) {
         ("Putting" + i.toString()).print();
         uux.put(i, i.copy()); 
      }
      
      var two = uux.copy();
      for (i = 0;i < uux.length;i = i++;) {
         if (uux.get(i) != two.get(i)) {
            "!FAILED copy at 1".print();
            return(false);
         }
      }
      two.put(3, 9);
      if (two.get(3) == uux.get(3)) {
         ("!FAILED copy at 2 " + two.get(3).toString() + " " + uux.get(3).toString()).print();
         return(false);
      }
      " PASSED copy".print();
      if (two != two) {
         "!FAILED not equals".print();
         return(false);
      } else {
         " PASSED not equals".print();
      }
      
      if (two == two) {
         " PASSED equals".print();
      } else {
         "!FAILED equals".print();
         return(false);
      }
      
      return(true);
   }
   
}

From the "current" directory, with enviroment setup:

TWX_StdLib.exe -buildFile Tests/buildFiles/testArray.txt

(... builds ...)

Running deployTest/TestArray/emit/c/CompileUnit/TWX_StdLibTest_TestArray.exe
 PASSED put, get
 PASSED iterate
Putting0
Putting1
Putting2
Putting3
Putting4
 PASSED copy
 PASSED not equals
 PASSED equals


Usage of the Boolean class:


class Test:TestBool {
   
   sub main() {
      return(testBool());
   }
   
   sub testBool() {
      var t = true;
      t = t!;
      if (t) {
         "!FAILED not".print();
         return(false);
      }
      " PASSED not".print();
      
      t = true;
      var f = false;
      if (t == f) {
         "!FAILED equals".print();
         return(false);
      }
      " PASSED equals".print();
      var ts = new(Logic:Bool, "true");
      if (ts) {
         " PASSED str cons".print();
      } else {
         "!FAILED str cons".print();
         return(false);
      }
      return(true);
   }
   
}

TWX_StdLib.exe -buildFile Tests/buildFiles/testBool.txt

(... builds ...)

Running deployTest/TestBool/emit/c/CompileUnit/TWX_StdLibTest_TestBool.exe
 PASSED not
 PASSED equals
 PASSED str cons



Trying out the Map Container (HashTable implementation):



use Container:Map;

class Test:TestMap {
   
   sub main() {
      return(testMap());
   }
   
   sub testMap() {
       
      self.hash.print();
      self.hash.print();
      
      if (self == self) {
         " SELF EQUALS".print();
      }
      
      var uux = new (Map);
      uux.put("Container:Array:Iterator", true);
      uux.rehash();
      if (uux.has("Container:Array:Iterator")) {
         "has true".print();
      } else {
         "has false".print();
      }
      uux.put("Container:Array:Iterator", true);
      if (testHas(uux, "Hi", false)) {
         uux.put("Hi", 0);
         if (testHas(uux, "Hi", true)) {
            var uuy = uux.copy();
            if (uuy.get("Hi") == 0) {
               uuy.put("Hi", 1);
               if (uux.get("Hi") == uuy.get("Hi")) {
                  "!FAILED copy2".print();
               } else {
                  " PASSED copy".print();
               }
            } else {
               "!FAILED copy 1".print();
            }
         } else {
            return(false);
         }
      } else {
         return(false);
      }
      return(testMapIter());
   }
   
   sub testMapIter() {
      var aax = new (Map);
      aax.put("Hi", "There");
      aax.put("Hi", "Boo");
      var cnt = 0;
      for (var i = aax.valueIterator;i.hasNext;;) {
         cnt = cnt++;
         i.next.print();
      }
      cnt.print();
   }
   
   sub testHas(uux, val, should) {
      var x = uux.has(val);
      if (x == should) {
      (" PASSED has " + val.toString() + " " + should.toString()).print();
      } else {
      ("!FAILED has " + val.toString() + " " + should.toString()).print();
      return(false);
      }
      
      return(true);
   }
   
}

TWX_StdLib.exe -buildFile Tests/buildFiles/testMap.txt

(... builds ...)

Running deployTest/TestMap/emit/c/CompileUnit/TWX_StdLibTest_TestMap.exe
134590392
134590392
 SELF EQUALS
has true
 PASSED has Hi false
 PASSED has Hi true
 PASSED copy
Boo
1



Performing IO and File operations:
Note: The first example block will not work with the existing 0.7 build, it works with the next build which will be delivered shortly


use IO:File;
use Text:String;

class Scratch:IOWork {
   sub main() {
   //readStdin();
   //writeStdout();
   
   
   var input = new(File, "In.txt").reader.open();
   //input.readAll().print();
   
   /*
   @String str = input.readLine();
   while (notNull(str)) {
      str.print();
      str = input.readLine();
   }
   */
   
   readLineStdin();
   
   //new(Text:Chars).newline.toCode().print();
   
   }
   
   sub readStdin() {
      var input = new(File:Reader:Stdin).open();
      input.readAll().print();
   }
   
   sub readLineStdin() {
      var input = new(File:Reader:Stdin).open();
      input.readLine().print();
   }
   
   sub writeStdout() {
      var output = new(File:Writer:Stdout).open();
      output.writeLine("Hi");
   }
}

TWX_StdLib.exe -buildFile ./Scratch/buildFiles/iowork.txt



use Math:Int;
use IO:File;

class Test:TestFile{
   
   sub main() {
      return(testFile());
   }
   
   sub testFile() {
   
      var contents;
      
      var file = new (File, "it.txt");
      var fileto = new (File, "to.txt");
      file.copyFile(fileto);
      contents = file.reader.open().readAll();
      file.close();
      contents.print();
      if (contents.length > 0) {
      (" PASSED fileRead").print();
      } else {
      "!FAILED fileRead".print();
      return(false);
      }
      
      return(true);
   }
   
}

TWX_StdLib.exe -buildFile Tests/buildFiles/testFile.txt

craig_welch2 – Sat, 2006 – 09 – 02 05:57