예제 #1
0
        public void TestBase16()
        {
            string       bek = @"
function E(x)=ite(x < 10, x + 48, x + 55);
function D(x)=ite(x < 58, x - 48, x - 55);

program base16encode(input){ 
  replace {
    @""[\0-\xFF]""  ==> [E(#0 >> 4),E(#0 & 0xF)];
  }
}

program base16decode(input){ 
  replace {
    ""[A-Z0-9]{2}"" ==> [(D(#0) << 4) + D(#1)];
  }
}
// Identity Encoder
program ID(_){replace { "".""   ==> [#0];}}
==
js(base16encode);
BYTES = regex(@""^[\0-\xFF]*$"");            //domain of sequences of bytes
ID_BYTES = restrict(ID,BYTES);             //identity over sequences of bytes
ed = join(base16encode, base16decode);     //composition, first encode then decode
eq(ID_BYTES, ed);   
";
            StringWriter sw  = new StringWriter();

            Pgm.Run(bek, sw, "");
            string s = sw.ToString();

            Console.WriteLine(s);
            Assert.IsTrue(s.Contains("Result: Equivalent"));
        }
예제 #2
0
        public void TestLocalFuncQuery()
        {
            string pgm = @"

function bar(x, y) = (y + x);
function foo(x) = bar(x,1);

program incr(w) {
   return iter(c in w) {
      case(((foo(c)-1)==c)&&true||false):
         yield(c + 0 + 1);
      };
}
==
js(incr);

";

            StringWriter sw = new StringWriter();

            Pgm.Run(pgm, sw, "bek_master.html");
            string s = sw.ToString();

            Console.WriteLine(s);
            Assert.IsTrue(s.Contains("foo"));
        }
예제 #3
0
        public void TestBase64Equiv()
        {
            string       bek = @"

function E(x)=(ite(x<=25,x+65,ite(x<=51,x+71,ite(x<=61,x-4,ite(x==62,'+','/')))));
function D(x)=(ite(x=='/',63,ite(x=='+',62,ite(x<='9',x+4,ite(x<='Z',x-65,x-71)))));
program base64encode(_){ 
  replace {
    @""[\0-\xFF]{3}""  ==> [E(Bits(7,2,#0)), E((Bits(1,0,#0)<<4)|Bits(7,4,#1)), E((Bits(3,0,#1)<<2)|Bits(7,6,#2)), E(Bits(5,0,#2))];
    @""[\0-\xFF]{2}$"" ==> [E(Bits(7,2,#0)), E((Bits(1,0,#0)<<4)|Bits(7,4,#1)), E(Bits(3,0,#1)<<2), '='];
    @""[\0-\xFF]$""    ==> [E(Bits(7,2,#0)), E(Bits(1,0,#0)<<4), '=', '='];
  }
}
program base64decode(_){ 
  replace {
    ""[a-zA-Z0-9+/]{4}""   ==> [(D(#0)<<2)|bits(5,4,D(#1)), (bits(3,0,D(#1))<<4)|bits(5,2,D(#2)), (bits(1,2,D(#2))<<6)|D(#3)];
    ""[a-zA-Z0-9+/]{3}=$"" ==> [(D(#0)<<2)|bits(5,4,D(#1)), (bits(3,0,D(#1))<<4)|bits(5,2,D(#2))];
    ""[a-zA-Z0-9+/]{2}==$""==> [(D(#0)<<2)|bits(5,4,D(#1))];
  }
}
program ID(w){return iter(x in w){case (true):yield(x);};}
==
BYTES = regex(@""^[\0-\xFF]*$"");            //domain of sequences of bytes
ID_BYTES = restrict(ID,BYTES);             //identity over sequences of bytes
ed = join(base64encode, base64decode);     //composition, first encode then decode
eq(ID_BYTES, ed); 
";
            StringWriter sw  = new StringWriter();

            Pgm.Run(bek, sw, "");
            string s = sw.ToString();

            Console.WriteLine(s);
            Assert.IsTrue(s.Contains("Equivalent"));
        }
예제 #4
0
        public void TestLooping()
        {
            string program = @"
program RegexEscape(_){ replace {
  ""\t"" ==> ""\\t"";
  ""\n"" ==> ""\\n"";
  ""\f"" ==> ""\\f"";
  ""\r"" ==> ""\\r"";
  @""(\ |\#|\$|\(|\)|\*|\+|\.|\?|\[|\\|\^|\{|\|)"" ==> ['\\',#0];
  else ==> [#0];
}}
==
a =join(RegexEscape,RegexEscape);
eqB(1,RegexEscape,a);
";

            StringWriter sw = new StringWriter();

            Pgm.Run(program, sw, "");
            Assert.IsTrue(sw.ToString().Contains("Not Partial-Equivalent"));
        }
예제 #5
0
        public void caesarcipher()
        {
            string sample = @"
// Caesar used a simple cipher 
// where all the letters were shifted by 3
function foo(x)= (x + 1 + 1);
program caesarcipher(w) { 
  return iter(c in w) {
    case(true): yield(c + 3);
  };
}
==
display(caesarcipher); js(caesarcipher);";

            StringWriter sw = new StringWriter();

            Pgm.Run(sample, sw, "../../bek.master.html");
            string s = sw.ToString();

            Console.WriteLine(s);
            Assert.IsTrue(s.Contains("foo"));
        }
예제 #6
0
        public void TestEndCase()
        {
            string program = @"

program alpha(w) {
   return iter(c in w) [ b := false; ] {
       case(c in " + "\"[a-zA-Z ]\"" + @"):
	     yield(c);
      } end { 
       case(true):
         yield(';');
      };
}

program beta(w) {
   return iter(c in w) [ b := false; ] {
      case(c in " + "\"[a-zA-Z ]\"" + @"):
	     yield(c);
      } end {
      case(!b):
        yield(';');
      };
}


==

image(alpha, " + "\"Hello World\"" + @");
eq(alpha,beta);

";

            StringWriter sw = new StringWriter();

            Pgm.Run(program, sw, "");
            string s = sw.ToString();

            Assert.IsTrue(s.Contains("Result: Equivalent"));
        }
예제 #7
0
        public void BasicDisplay()
        {
            string program = @"
function dec(x)=(x-48);
program alpha(w) {
   return iter(c in w) [ b := false; ] {
      case(true):
	     yield(dec(c), ' ');
      };
}

==

display(alpha);";

            StringWriter sw = new StringWriter();

            Pgm.Run(program, sw, "bek_master.html");
            string s = sw.ToString();

            Console.WriteLine(s);
            Assert.IsTrue(s.Contains("Fig_1"));
        }
예제 #8
0
        public void TestOutput()
        {
            string program = @"

program alpha(w) {
   return iter(c in w) [ b := false; ] {
      case(c in " + "\"[a-zA-Z ]\"" + @"):
	     yield(c);
      end case(true):
        yield(';');
      };
}

==
display(inv(alpha, " + "\"Hello World;\"" + @"));";

            StringWriter sw = new StringWriter();

            Pgm.Run(program, sw, "");
            string s = sw.ToString();

            Assert.IsTrue(s.Contains("Fig_1"));
        }
예제 #9
0
        public void TestPreimage()
        {
            string       program = @"
program alpha(w) {
   return iter(c in w) {
      case(c == 'Y') :
         yield('H');
      case((c in " + "\"[a-zA-Z ]\"" + @") && (c != 'H')):
	     yield(c);
      end case(true):
        yield(';');
      };
}
==
display(invimage(alpha," + "\"Hello World\"" + @"));
";
            StringWriter sw      = new StringWriter();

            Pgm.Run(program, sw, "");
            string s = sw.ToString();

            Assert.IsTrue(s.Contains("Fig_1"));
        }
예제 #10
0
        public void TestImage()
        {
            string program = @"

program alpha(w) {
   return iter(c in w) {
      case((c == 'h') || (c == 'l')):
	     yield('X');
      case(c == 'o'):
         yield(c);
      case(true): 
         yield('i');
      };
}

==
image(alpha, " + "\"hello\"" + @");
";

            StringWriter sw = new StringWriter();

            Pgm.Run(program, sw, "");
            Assert.IsTrue(sw.ToString().Contains("XiXXo"));
        }