⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 headtailexpressiontest.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
字号:
/* 
 * Local Variables:
 * c-basic-offset:2
 * tab-width:2
 * indent-tabs-mode:nil
 * End:
 */
package com.sri.oaa2.test.icl;

import com.sri.oaa2.icl.*;
import com.sri.oaa2.simplefac.*;
import java.net.*;
import java.io.*;

import junit.framework.TestCase;

/**
 * @author agno
 *
 */
public class HeadTailExpressionTest extends TestCase 
{
  public HeadTailExpressionTest(String arg0)
  {
    super(arg0);
  }

  public void testOneTermHeadVariableTail() 
  {
    IclTerm t;
    try {
      t = IclTerm.fromString("[a|V]");
    }
    catch(Exception e) {
      System.out.println("Bad string: " + "[a|V]" + " " + e.toString());
      e.printStackTrace();
      TestCase.assertTrue(false);
      return;
    }

    String s = t.toString();
    TestCase.assertEquals("[a|V]", s);
  }

  public void testTwoTermHeadVariableTail() 
  {
    IclTerm t;
    try {
      t = IclTerm.fromString("[a,b|V]");
    }
    catch(Exception e) {
      System.out.println("Bad string: " + "[a,b|V]" + " " + e.toString());
      e.printStackTrace();
      TestCase.assertTrue(false);
      return;
    }

    String s = t.toString();
    TestCase.assertEquals("[a,b|V]", s);
  }

  public void testOneTermHeadListTail()
  {
    IclTerm t;
    try {
      t = IclTerm.fromString("[a|[a,b]]");
    }
    catch(Exception e) {
      System.out.println("Bad string: " + "[a|[a,b]]" + " " + e.toString());
      e.printStackTrace();
      TestCase.assertTrue(false);
      return;
    }

    String s = t.toString();
    TestCase.assertEquals(3, t.size());
    TestCase.assertEquals("[a,a,b]", s);    
  }

  public void testNestedSimple()
  {
    IclTerm t;
    try {
      t = IclTerm.fromString("[[a|B]|[a|[a|B]]]");
    }
    catch(Exception e) {
      System.out.println("Bad string: " + "[[a|B]|[a|[a|B]]]" + " " + e.toString());
      e.printStackTrace();
      TestCase.assertTrue(false);
      return;
    }

    String s = t.toString();
    TestCase.assertEquals("[[a|B],a,a|B]", s);    
  }

  public void testNestedMulti()
  {
    IclTerm t;
    try {
      t = IclTerm.fromString("[[a,b,c|D]|[e,f,g,[h|I]|[j,k,l,m,n|O]]]");
    }
    catch(Exception e) {
      System.out.println("Bad string: " + "[[a,b,c|D]|[e,f,g,[h|I]|[j,k,l,m,n|O]]]" + " " + e.toString());
      e.printStackTrace();
      TestCase.assertTrue(false);
      return;
    }

    String s = t.toString();
    TestCase.assertEquals("[[a,b,c|D],e,f,g,[h|I],j,k,l,m,n|O]", s);
  }

  public void testNoVar() 
  {
    IclTerm t;
    try {
      t = IclTerm.fromString("[a|b]");
    }
    catch(Exception e) {
      return;
    }
    System.out.println("Bad string: " + "[a|b] parsed, but is illegal");
    TestCase.assertTrue(false);
  }
  
  public void testUnificationSameLength()
  {
    IclList a = (IclList)IclTerm.fromString(true, "[a,b,c|X]");
    IclList b = (IclList)IclTerm.fromString(true, "[a,b,c,d]");
    
    IclTerm u = Unifier.getInstance().unify(a, b);
    TestCase.assertNotNull(u);
    TestCase.assertEquals("[a,b,c,d]", u.toString());
  }

  public void testUnificationLongerHasTail()
  {
    IclList a = (IclList)IclTerm.fromString(true, "[a|X]");
    IclList b = (IclList)IclTerm.fromString(true, "[a]");
    
    IclTerm u = Unifier.getInstance().unify(a, b);
    TestCase.assertNotNull(u);
    TestCase.assertEquals("[a]", u.toString());
  }

  public void testUnificationShorterHasTail()
  {
    IclList a = (IclList)IclTerm.fromString(true, "[a|X]");
    IclList b = (IclList)IclTerm.fromString(true, "[a,b,c]");
    
    IclTerm u = Unifier.getInstance().unify(a, b);
    TestCase.assertNotNull(u);
    TestCase.assertEquals("[a,b,c]", u.toString());
  }

  public void testUnificationShorterHasTailComplex()
  {
    IclList a = (IclList)IclTerm.fromString(true, "[a|X]");
    IclList b = (IclList)IclTerm.fromString(true, "[a,[b|C],d,e(f([g,h|I])),[j|K]]");
    
    IclTerm u = Unifier.getInstance().unify(a, b);
    TestCase.assertNotNull(u);
    TestCase.assertEquals("[a,[b|C],d,e(f([g,h|I])),[j|K]]", u.toString());
  }

  public void testTailCreation() 
  {
    IclList l = new IclList(new IclStr("a"));
    l.setTail(new IclVar("X"));
    TestCase.assertEquals("[a|X]", l.toString());
  }

  public void testInvalidTail()
  {
    IclList l = new IclList(new IclStr("a"));
    try {
      l.setTail(new IclStr("b"));
    }
    catch(IllegalArgumentException iae) {
      return;
    }
    System.err.println("Created IclList with IclStr as tail");
    TestCase.assertTrue(false);
  }

  public void testTailCompressAPI() 
  {
    IclList abcd = (IclList)IclTerm.fromString(true, "[a, b, c]");
    abcd.setTail(IclTerm.fromString(true, "D"));
    IclList hi = (IclList)IclTerm.fromString(true, "[h]");
    hi.setTail(IclTerm.fromString(true, "I"));
    IclList e_to_o = (IclList)IclTerm.fromString(true, "[e,f,g]");
    e_to_o.add(hi);
    IclList j_to_o = (IclList)IclTerm.fromString(true, "[j,k,l,m,n]");
    j_to_o.setTail(new IclVar("O"));
    e_to_o.setTail(j_to_o);
    IclList all = new IclList();
    all.add(abcd);
    all.setTail(e_to_o);

    TestCase.assertEquals("[[a,b,c|D],e,f,g,[h|I],j,k,l,m,n|O]", all.toString());
  }

  public void testBinaryParsing() throws InterruptedException, IOException
  {
    final String[] strings = 
      { "[a|V]",
        "[a,b|V]",
        "[[a|B],a,a|B]",
        "[[a,b,c|D],e,f,g,[h|I],j,k,l,m,n|O]",
      }
    ;
    
    final PipedOutputStream pos = new PipedOutputStream();
    final PipedInputStream pis = new PipedInputStream();
    pos.connect(pis);
    final DataInputStream dis = new DataInputStream(pis);
    final BufferedOutputStream bos = new BufferedOutputStream(pos);

    final BinaryFormatWriter writer = new BinaryFormatWriter(bos, 8192);
    final BinaryFormatReader reader = new BinaryFormatReader(dis);

    Thread writeThread = new Thread() 
      {
        public void run()
        {
          IclTerm t;
          try {
            for(int i = 0; i < strings.length; ++i) {
              t = IclTerm.fromString(strings[i]);
              writer.write(t);
            }
          }
          catch(Exception e) {
            throw new RuntimeException(e);
          }
        }
      };

    writeThread.start();
    
    Thread readThread = new Thread() 
      {
        public void run()
        {
          try {
            IclTerm t;
            for(int i = 0; i < strings.length; ++i) {
              t = reader.read();
              TestCase.assertEquals(strings[i], t.toString());
            }
          }
          catch(IOException ioe) {
            throw new RuntimeException(ioe);
          }
        }
      };

    readThread.start();
    
    writeThread.join();
    readThread.join();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -