-- TestTimeExtra.mesa,  HGM,  February 16, 1981  3:26 PM  

DIRECTORY
  Process USING [Detach],
  Put USING [CR, Char, Line, Text],
  Runtime USING [CallDebugger],
  String USING [EquivalentStrings],
  System USING [AdjustGreenwichMeanTime],
  Time USING [Append, Pack, Packed, Unpack, Unpacked],
  TimeExtra USING [PackedTimeFromString];

TestTimeExtra: PROGRAM
  IMPORTS Put, Process, Runtime, String, System, Time, TimeExtra =
  BEGIN

  first, second: BOOLEAN ← TRUE;

  StrangeCase: PROCEDURE [s: STRING] =
    BEGIN
    p1: Time.Packed ← System.AdjustGreenwichMeanTime[
      TimeExtra.PackedTimeFromString[s], 7200];
    p2: Time.Packed ← System.AdjustGreenwichMeanTime[p1, 3600];
    p3: Time.Packed ← System.AdjustGreenwichMeanTime[p2, 3600];
    p4: Time.Packed ← System.AdjustGreenwichMeanTime[p3, 3600];
    u1: Time.Unpacked ← Time.Unpack[p1];
    u2: Time.Unpacked ← Time.Unpack[p2];
    u3: Time.Unpacked ← Time.Unpack[p3];
    u4: Time.Unpacked ← Time.Unpack[p4];
    r1, r2, r3, r4, r5, r6, r7, r8: Time.Packed;
    t1: STRING = [22];
    t2: STRING = [22];
    t3: STRING = [22];
    t4: STRING = [22];
    t5: STRING = [22];
    t6: STRING = [22];
    t7: STRING = [22];
    t8: STRING = [22];
    r1 ← Time.Pack[u1];
    r2 ← Time.Pack[u2];
    r3 ← Time.Pack[u3];
    r4 ← Time.Pack[u4];
    r5 ← Time.Pack[u1, FALSE];
    r6 ← Time.Pack[u2, FALSE];
    r7 ← Time.Pack[u3, FALSE];
    r8 ← Time.Pack[u4, FALSE];
    IF first THEN Runtime.CallDebugger["Ok, watch these......!"];
    Time.Append[t1, Time.Unpack[r1], TRUE];
    Time.Append[t2, Time.Unpack[r2], TRUE];
    Time.Append[t3, Time.Unpack[r3], TRUE];
    Time.Append[t4, Time.Unpack[r4], TRUE];
    Time.Append[t5, Time.Unpack[r5], TRUE];
    Time.Append[t6, Time.Unpack[r6], TRUE];
    Time.Append[t7, Time.Unpack[r7], TRUE];
    Time.Append[t8, Time.Unpack[r8], TRUE];
    IF second THEN Runtime.CallDebugger["Looks like a time conversion screwup!"];
    END;

  TestOne: PROCEDURE [s: STRING] =
    BEGIN
    temp, temp2: Time.Packed;
    t: STRING = [22];
    Put.Text[NIL, s];
    Put.Text[NIL, " => "];
    temp ← TimeExtra.PackedTimeFromString[s];
    IF temp = 0 THEN GOTO Invalid;
    Time.Append[t, Time.Unpack[temp]];
    Put.Text[NIL, t];
    temp2 ← TimeExtra.PackedTimeFromString[s];
    IF temp # temp2 THEN GOTO Screwup;
    IF ~String.EquivalentStrings[s, t] THEN StrangeCase[s];
    Put.Line[NIL, "."];
    EXITS
      Invalid => Put.Line[NIL, "Invalid..."];
      Screwup => Put.Line[NIL, "Didn't get the same answer..."];
    END;

  MainTest: PROCEDURE =
    BEGIN
    TestOne[" 1-Jan-30  0:00:00"];
    TestOne[" 1-Feb-80  0:00:00"];
    TestOne[" 1-Mar-80  1:00:00"];
    TestOne[" 1-Apr-80  1:00:00"];
    TestOne["27-Apr-80  3:10:13"];
    TestOne[" 1-May-80 14:13:37"];
    TestOne[" 1-Jun-80 12:00:00"];
    TestOne[" 1-Jul-80 24:00:00"];
    TestOne[" 1-Aug-80  1:00:00"];
    TestOne[" 1-Sep-80  1:00:00"];
    TestOne["26-Oct-80  1:06:00"];
    TestOne["26-Oct-80  2:06:00"];
    TestOne["25-Oct-81  3:03:00"];
    TestOne[" 1-Nov-80  1:00:00"];
    TestOne[" 1-Dec-80  1:00:00"];
    Put.CR[NIL];
    Put.CR[NIL];

    BEGIN
    i: Time.Packed ← TimeExtra.PackedTimeFromString[" 1-Apr-80  0:00:00"];
    seeThings: CARDINAL ← 0;
    UNTIL (i MOD 10) = 0 DO i ← System.AdjustGreenwichMeanTime[i, 1]; ENDLOOP;
    UNTIL (i MOD 100) = 0 DO i ← System.AdjustGreenwichMeanTime[i, 10]; ENDLOOP;
    UNTIL (i MOD 1000) = 0 DO i ← System.AdjustGreenwichMeanTime[i, 100]; ENDLOOP;
    DO
      x: STRING = [18];
      y: Time.Packed;
      Time.Append[x, Time.Unpack[i]];
      y ← TimeExtra.PackedTimeFromString[x];
      IF y # i OR seeThings = 0 THEN
	BEGIN
	u1, u2: Time.Unpacked;
	u1 ← Time.Unpack[i];
	u2 ← Time.Unpack[y];
	Put.Text[NIL, x];
	IF y # i THEN Put.Text[NIL, "  ******"];
	IF y # i THEN StrangeCase[x];
	Put.Line[NIL, "."];
	seeThings ← 1000;
	END;
      i ← System.AdjustGreenwichMeanTime[i, 1000];
      seeThings ← seeThings - 1;
      IF i = LAST[LONG CARDINAL] THEN EXIT;
      ENDLOOP;
    END;
    END;

  Process.Detach[FORK MainTest[]];
  END.