Source Code

<script type="text/javascript" src="ipnum.js"></script>
<script type="text/javascript">
  //<![CDATA[

  // output
  function output(text) {
      var output = document.getElementById('output');
      output.appendChild(document.createTextNode(text));
      output.appendChild(document.createElement('br'));
  }

  // test get/set array
  function testGetSetArray() {
      var o = 'test getArray and setArray: ';
      var a1 = new Array(3);
      a1[0] = new Array();
      a1[1] = new Array();
      a1[2] = 1;
      a1[0].push(1);
      a1[0].push(2);
      a1[0].push(3);
      a1[1].push(5);
      a1[1].push(6);
      a1[1].push(7);
      var i1 = new IpNum();
      i1.setArray(a1);
      var a2 = i1.getArray();
      var i2 = new IpNum();
      i2.setArray(a2);
      o += i1.getString() + ' = ' + i2.getString();
      if (i1.equals(i2)) o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
  }

  // test get/set string
  function testGetSetString() {
      var o = 'test getString and setString: ';
      var s1 = '123.456';
      var i1 = new IpNum();
      i1.setString(s1);
      var s2 = i1.getString();
      o += s1 + ' = ' + s2;
      if (s1 == s2) o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
      o = 'test getString and setString: ';
      var s3 = '0';
      var i2 = new IpNum();
      i2.setString(s3);
      var s4 = i2.getString();
      o += s3 + ' = ' + s4;
      if (s3 == s4) o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
  }

  // test equals
  function testEquals() {
      var o = 'test equals: ';
      var s1 = '123.456';
      var i1 = new IpNum();
      i1.setString(s1);
      var s2 = '123.456';
      var i2 = new IpNum();
      i2.setString(s2);
      o += s1 + ' = ' + s2;
      if (i1.equals(i2)) o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
      o = 'test equals: ';
      var s3 = '123.456';
      var i3 = new IpNum();
      i3.setString(s3);
      var s4 = '123.457';
      var i4 = new IpNum();
      i4.setString(s4);
      o += s3 + ' != ' + s4;
      if (!(i3.equals(i4))) o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
  }

  // test abs
  function testAbs() {
      var o = 'test abs: ';
      var s1 = '123.456';
      var i1 = new IpNum();
      i1.setString(s1);
      i2 = i1.abs();
      var s2 = i2.getString();
      o += 'abs(' + s1 + ')' + ' = ' + s2;
      if (s2 == '123.456') o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
      o = 'test abs: ';
      var s3 = '-123.456';
      var i3 = new IpNum();
      i3.setString(s3);
      i4 = i3.abs();
      var s4 = i4.getString();
      o += 'abs(' + s3 + ')' + ' = ' + s4;
      if (s4 == '123.456') o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
  }

  // test gt
  function testGt() {
      var o = 'test gt: ';
      var s1 = '123.456';
      var i1 = new IpNum();
      i1.setString(s1);
      var s2 = '123.455';
      var i2 = new IpNum();
      i2.setString(s2);
      o += s1 + ' > ' + s2;
      if (i1.gt(i2)) o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
      o = 'test gt: ';
      var s3 = '123.456';
      var i3 = new IpNum();
      i3.setString(s3);
      var s4 = '123.456';
      var i4 = new IpNum();
      i4.setString(s4);
      o += s3 + ' !> ' + s4;
      if (!(i3.gt(i4))) o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
  }

  // test lt
  function testLt() {
      var o = 'test lt: ';
      var s1 = '-123.456';
      var i1 = new IpNum();
      i1.setString(s1);
      var s2 = '-123.455';
      var i2 = new IpNum();
      i2.setString(s2);
      o += s1 + ' < ' + s2;
      if (i1.lt(i2)) o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
      o = 'test lt: ';
      var s3 = '-123.456';
      var i3 = new IpNum();
      i3.setString(s3);
      var s4 = '-123.456';
      var i4 = new IpNum();
      i4.setString(s4);
      o += s3 + ' !< ' + s4;
      if (!(i3.lt(i4))) o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
  }

  // test gte
  function testGte() {
      var o = 'test gte: ';
      var s1 = '123.456';
      var i1 = new IpNum();
      i1.setString(s1);
      var s2 = '123.455';
      var i2 = new IpNum();
      i2.setString(s2);
      o += s1 + ' >= ' + s2;
      if (i1.gte(i2)) o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
      o = 'test gte: ';
      var s3 = '123.456';
      var i3 = new IpNum();
      i3.setString(s3);
      var s4 = '123.456';
      var i4 = new IpNum();
      i4.setString(s4);
      o += s3 + ' >= ' + s4;
      if (i3.gte(i4)) o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
  }

  // test lte
  function testLte() {
      var o = 'test lte: ';
      var s1 = '-123.456';
      var i1 = new IpNum();
      i1.setString(s1);
      var s2 = '-123.455';
      var i2 = new IpNum();
      i2.setString(s2);
      o += s1 + ' <= ' + s2;
      if (i1.lte(i2)) o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
      o = 'test lte: ';
      var s3 = '-123.456';
      var i3 = new IpNum();
      i3.setString(s3);
      var s4 = '-123.456';
      var i4 = new IpNum();
      i4.setString(s4);
      o += s3 + ' <= ' + s4;
      if (i3.lte(i4)) o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
  }

  // test add
  function testAdd() {
      var o = 'test add: ';
      var s1 = '123.456';
      var i1 = new IpNum();
      i1.setString(s1);
      var s2 = '78.9';
      var i2 = new IpNum();
      i2.setString(s2);
      o += s1 + '+' + s2 + ' = 202.356';
      if (i1.add(i2).getString() == '202.356') o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
      o = 'test add: ';
      var s3 = '123.456';
      var i3 = new IpNum();
      i3.setString(s3);
      var s4 = '-789.01';
      var i4 = new IpNum();
      i4.setString(s4);
      o += s3 + '+' + s4 + ' = -665.554';
      if (i3.add(i4).getString() == '-665.554') o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
      o = 'test add: ';
      var s5 = '-123.456';
      var i5 = new IpNum();
      i5.setString(s5);
      var s6 = '131.01';
      var i6 = new IpNum();
      i6.setString(s6);
      o += s5 + '+' + s6 + ' = 7.554';
      if (i5.add(i6).getString() == '7.554') o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
  }

  // test sub
  function testSub() {
      var o = 'test sub: ';
      var s1 = '123.456';
      var i1 = new IpNum();
      i1.setString(s1);
      var s2 = '78.9';
      var i2 = new IpNum();
      i2.setString(s2);
      o += s1 + '-' + s2 + ' = 44.556';
      if (i1.sub(i2).getString() == '44.556') o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
      o = 'test sub: ';
      var s3 = '123.456';
      var i3 = new IpNum();
      i3.setString(s3);
      var s4 = '-789.01';
      var i4 = new IpNum();
      i4.setString(s4);
      o += s3 + '-' + s4 + ' = 912.466';
      if (i3.sub(i4).getString() == '912.466') o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
      o = 'test sub: ';
      var s5 = '-123.456';
      var i5 = new IpNum();
      i5.setString(s5);
      var s6 = '131.01';
      var i6 = new IpNum();
      i6.setString(s6);
      o += s5 + '-' + s6 + ' = -254.466';
      if (i5.sub(i6).getString() == '-254.466') o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
  }

  // test mul
  function testMul() {
      var o = 'test mul: ';
      var s1 = '123.456';
      var i1 = new IpNum();
      i1.setString(s1);
      var s2 = '78.9';
      var i2 = new IpNum();
      i2.setString(s2);
      o += s1 + '*' + s2 + ' = 9740.6784';
      if (i1.mul(i2).getString() == '9740.6784') o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
      o = 'test mul: ';
      var s3 = '123.456';
      var i3 = new IpNum();
      i3.setString(s3);
      var s4 = '-789.01';
      var i4 = new IpNum();
      i4.setString(s4);
      o += s3 + '*' + s4 + ' = -97408.01856';
      if (i3.mul(i4).getString() == '-97408.01856') o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
      o = 'test mul: ';
      var s5 = '-123.456';
      var i5 = new IpNum();
      i5.setString(s5);
      var s6 = '-0.01';
      var i6 = new IpNum();
      i6.setString(s6);
      o += s5 + '*' + s6 + ' = 1.23456';
      if (i5.mul(i6).getString() == '1.23456') o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
  }

  // test fact
  function testFact() {
      var o = 'test fact: ';
      var s1 = '123.456';
      var i1 = new IpNum();
      i1.setString(s1);
      o += 'fact(' + s1 + ') = undefined';
      if (i1.fact() == undefined) o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
      o = 'test fact: ';
      var s2 = '0';
      var i2 = new IpNum();
      i2.setString(s2);
      o += 'fact(' + s2 + ') = 1';
      if (i2.fact().getString() == '1') o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
      o = 'test fact: ';
      var s3 = '5';
      var i3 = new IpNum();
      i3.setString(s3);
      o += 'fact(' + s3 + ') = 120';
      if (i3.fact().getString() == '120') o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
  }

  // test floor
  function testFloor() {
      var o = 'test floor: ';
      var s1 = '123.456';
      var i1 = new IpNum();
      i1.setString(s1);
      i2 = i1.floor();
      var s2 = i2.getString();
      o += 'floor(' + s1 + ')' + ' = ' + s2;
      if (s2 == '123') o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
      o = 'test floor: ';
      var s3 = '-123.456';
      var i3 = new IpNum();
      i3.setString(s3);
      i4 = i3.floor();
      var s4 = i4.getString();
      o += 'floor(' + s3 + ')' + ' = ' + s4;
      if (s4 == '-123') o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
  }

  // test ceil
  function testCeil() {
      var o = 'test ceil: ';
      var s1 = '123.456';
      var i1 = new IpNum();
      i1.setString(s1);
      i2 = i1.ceil();
      var s2 = i2.getString();
      o += 'ceil(' + s1 + ')' + ' = ' + s2;
      if (s2 == '124') o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
      o = 'test ceil: ';
      var s3 = '-123.456';
      var i3 = new IpNum();
      i3.setString(s3);
      i4 = i3.ceil();
      var s4 = i4.getString();
      o += 'ceil(' + s3 + ')' + ' = ' + s4;
      if (s4 == '-124') o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
  }

  // test round
  function testRound() {
      var o = 'test round: ';
      var s1 = '123.456';
      var i1 = new IpNum();
      i1.setString(s1);
      i2 = i1.round();
      var s2 = i2.getString();
      o += 'round(' + s1 + ')' + ' = ' + s2;
      if (s2 == '123') o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
      o = 'test round: ';
      var s3 = '-123.789';
      var i3 = new IpNum();
      i3.setString(s3);
      i4 = i3.round();
      var s4 = i4.getString();
      o += 'round(' + s3 + ')' + ' = ' + s4;
      if (s4 == '-124') o += ' [OK]';
      else o += ' [FAIL]';
      output(o);
  }

  // tests
  function tests() {
      testGetSetArray();
      testGetSetString();
      testEquals();
      testAbs();
      testGt();
      testLt();
      testGte();
      testLte();
      testAdd();
      testSub();
      testMul();
      testFact();
      testFloor();
      testCeil();
      testRound();
  }

  window.onload = tests;
  //]]>
</script>

<div id="output">
</div>