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

📄 iclutilstest.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
字号:
package com.sri.oaa2.test.icl;

import com.sri.oaa2.icl.IclDataQ;
import com.sri.oaa2.icl.IclList;
import com.sri.oaa2.icl.IclTerm;
import com.sri.oaa2.icl.IclUtils;
import junit.framework.Assert;
import junit.framework.TestCase;

/**
 * Author: Chris Brigham (cbrigham)
 * Date: Jun 30, 2004
 * Time: 1:19:46 PM
 * Description:
 */
public class IclUtilsTest extends TestCase {
    public IclUtilsTest(String arg) {
        super(arg);
    }

    public void testDoubleQuotes() {
        Assert.assertEquals(IclUtils.doubleQuotes(""), "");
        Assert.assertEquals(IclUtils.doubleQuotes("'"), "''");
        Assert.assertEquals(IclUtils.doubleQuotes("isn't"), "isn''t");
        Assert.assertEquals(IclUtils.doubleQuotes("\""), "\"");
        Assert.assertEquals(IclUtils.doubleQuotes("a couple of words"), "a couple of words");
        Assert.assertEquals(IclUtils.doubleQuotes("a 'quoted' word"), "a ''quoted'' word");
        Assert.assertEquals(IclUtils.doubleQuotes("'a quoted phrase'"), "''a quoted phrase''");

    }

    public void testFixQuotes() {
        Assert.assertEquals(IclUtils.fixQuotes(""), "");
        Assert.assertEquals(IclUtils.fixQuotes("''"), "");
        Assert.assertEquals(IclUtils.fixQuotes("isn''t it"), "isn't it");
        Assert.assertEquals(IclUtils.fixQuotes("'isn''t it'"), "isn't it");
        Assert.assertEquals(IclUtils.fixQuotes("''''"), "'");
    }

    public void testFromString() {
        String[] strings = {
            "a",
            "1",
            "1.0",
            "V",
            "goal(_x,_y,_z)",
            "_",
            "hello(there)",
            "{hello}",
            "[hello]",
            "'spaces spaces'",
            "a + b",
            "a :- b",
            ":-(a,b)",
            "{hello,there}",
            "[hello, there]",
            "{g1, 5, _9}",
            "'blah''blah'",
            "hello([there,blah, blah],'blah''blah',{1.5, another(struct,[nested])})",
            "icldataq(\"blah blah\")",
            "<(_6025,43)",
            "[a|V]"
        };

        for (int i = 0; i < strings.length; ++i) {
            try {
                IclTerm t = IclTerm.fromString(strings[i]);
                Assert.assertTrue(t != null);
                //System.out.println("test string #" + i + ": " + t.toString());

                /* toString() for IclDataQ objects is treated differently, so test must be different */
                if (t instanceof IclDataQ) {
                    //System.out.println("reparse: " + ((IclDataQ) t).getQuotedData());
                    Assert.assertTrue("parsed term does not reparse to equal input",
                            t.equals(IclTerm.fromString(((IclDataQ) t).getQuotedData())));
                } else {
                    Assert.assertTrue("parsed term does not reparse to equal input",
                            t.equals(IclTerm.fromString(t.toString())));
                }
            } catch (Exception e) {
                System.out.println("Bad String " + strings[i] + " " + e.toString());
                e.printStackTrace();
                TestCase.assertTrue(false);
            }
        }

        IclTerm t = IclTerm.fromString(true, "");
        TestCase.assertTrue(t == null);

    }

    public void testGetMember() {
        IclTerm list = IclTerm.fromString(true, "[eats(dog,cat),eats(cat,mouse),eats(dog,mouse),eats(dog,squirrel)]");
        IclTerm elmt = IclTerm.fromString(true, "eats(dog,Animal)");
        IclTerm elmt2 = IclTerm.fromString(true, "eats(horse,Animal)");

        Assert.assertTrue(IclUtils.getMember(null, null) == null);
        Assert.assertTrue(IclUtils.getMember(elmt, null) == null);
        Assert.assertTrue(IclUtils.getMember(null, list) == null);
        Assert.assertTrue(IclUtils.getMember(elmt, elmt2) == null);
        Assert.assertTrue(IclUtils.getMember(elmt, list).equals(IclTerm.fromString(true, "eats(dog,cat)")));
        Assert.assertTrue(IclUtils.getMember(elmt2, list) == null);
    }

    public void testGetParamValue() {
        IclList params = (IclList) IclTerm.fromString(true, "[cache(false),level_limit(2),destructo]");
        IclTerm defaultVal = IclTerm.fromString(true, "not_found");

        Assert.assertEquals(IclUtils.getParamValue("cache", defaultVal, params), IclTerm.fromString(true, "false"));
        Assert.assertEquals(IclUtils.getParamValue("level_limit", defaultVal, params), IclTerm.fromString(true, "2"));
        Assert.assertEquals(IclUtils.getParamValue("destructo", defaultVal, params), IclTerm.fromString(true, "true"));
        Assert.assertEquals(IclUtils.getParamValue("no_exist", defaultVal, params), defaultVal);
        Assert.assertEquals(IclUtils.getParamValue("cache", defaultVal, null), defaultVal);

    }

    public void testGetTrueAtom() {
        Assert.assertEquals(IclUtils.getTrueAtom(), IclTerm.fromString(true, "true"));
    }

    public void testRemoveQuotes() {
        Assert.assertEquals(IclUtils.removeQuotes(""), "");
        Assert.assertEquals(IclUtils.removeQuotes("'lopsided"), "lopsided");
        Assert.assertEquals(IclUtils.removeQuotes("lopsided'"), "lopsided");
        Assert.assertEquals(IclUtils.removeQuotes("'quoted'"), "quoted");
        Assert.assertEquals(IclUtils.removeQuotes("'words in quotes'"), "words in quotes");
        Assert.assertEquals(IclUtils.removeQuotes("inner 'yes' quotes"), "inner 'yes' quotes");
        Assert.assertEquals(IclUtils.removeQuotes("\"doubles\""), "doubles");
        Assert.assertEquals(IclUtils.removeQuotes("\"crazy'"), "crazy");
        Assert.assertEquals(IclUtils.removeQuotes("'\"'''\"\"'\""), "\"'''\"\"'");
    }

    public void testUndoubleQuotes() {
        Assert.assertEquals(IclUtils.undoubleQuotes(""), "");
        Assert.assertEquals(IclUtils.undoubleQuotes("'isn't'"), "'isn't'");
        Assert.assertEquals(IclUtils.undoubleQuotes("''test'' isn''t 'good'"), "'test' isn't 'good'");
        Assert.assertEquals(IclUtils.undoubleQuotes("''''''"), "'''");
        Assert.assertEquals(IclUtils.undoubleQuotes("'''''''"), "''''");
    }

    public void testTermParamValue() {
        IclTerm expectVar = IclTerm.fromString(true, "X");
        IclTerm expectInt = IclTerm.fromString(true, "2");
        IclTerm expectDeep = IclTerm.fromString(true, "deep(_)");
        IclTerm params = IclTerm.fromString(true, "[cache(false),level_limit(3),level_limit(2),destructo,deep(deep(deep(true)))]");

        Assert.assertEquals(IclUtils.termParamValue("cache", null, params), IclTerm.fromString(true, "cache(false)"));
        Assert.assertEquals(IclUtils.termParamValue("cache", expectInt, params), null);
        Assert.assertEquals(IclUtils.termParamValue("destructo", null, params), IclTerm.fromString(true, "destructo(true)"));
        Assert.assertEquals(IclUtils.termParamValue("nothing", null, params), null);
        Assert.assertEquals(IclUtils.termParamValue("level_limit", expectVar, params), IclTerm.fromString(true, "level_limit(3)"));
        Assert.assertEquals(IclUtils.termParamValue("level_limit", expectInt, params), IclTerm.fromString(true, "level_limit(2)"));
        Assert.assertEquals(IclUtils.termParamValue("deep", expectDeep, params), IclTerm.fromString(true, "deep(deep(deep(true)))"));
    }

    public void testIntParamValue() {
        IclTerm params = IclTerm.fromString(true, "[cache(false),level_limit(3),level_limit(2),destructo,deep(deep(deep(true)))]");

        Assert.assertEquals(IclUtils.intParamValue("cache", 356, params), 356);
        Assert.assertEquals(IclUtils.intParamValue("cache", Integer.MIN_VALUE, params), Integer.MIN_VALUE);
        Assert.assertEquals(IclUtils.intParamValue("nothing", Integer.MAX_VALUE, params), Integer.MAX_VALUE);
        Assert.assertEquals(IclUtils.intParamValue("level_limit", 1024, params), 3);
    }

    public void testRemoveParamValue() {
        IclList params = (IclList) IclTerm.fromString(true, "[cache(false),level_limit(3),level_limit(2),destructo,deep(deep(deep(true)))]");

        Assert.assertTrue(IclUtils.removeParamValue("cache",params));
        Assert.assertFalse(IclUtils.removeParamValue("cache",params));
        //Assert.assertTrue(params.size() == 4);
        Assert.assertTrue(IclUtils.removeParamValue("level_limit",params));
        Assert.assertFalse(IclUtils.removeParamValue("level_limit",params));
        //Assert.assertTrue(params.size() == 2);
    }



}

⌨️ 快捷键说明

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