Exemple #1
0
int main()
{
    try {
        poc();
        TestSection("LispEvaluations");
        TestSubsection("ConstantEvaluations");
        {
            LReference intref(25);
            TESTC("int_evaluating", intref.Evaluate(), intref);
            LReference floatref(25.55);
            TESTC("float_evaluating", floatref.Evaluate(), floatref);
            LReference stringref("string");
            TESTC("string_evaluating", stringref.Evaluate(), stringref);

        }
        poc();
        TestSubsection("LSymbol");
        {
            LSymbol A("A");
            LReference nullref;
            TESTC("unassigned_symbol", A->GetDynamicValue(), nullref);
            LReference ref(2552);
            A->SetDynamicValue(ref);
            TESTC("dynamic_value", A->GetDynamicValue(), ref);
            TESTC("dynamic_value_eval", LReference(A).Evaluate(), ref);
        }
        poc();
        TestSubsection("LExpressionContext");
        {
            LSymbol SS("SS");
            LReference ref25(25);
            SS->SetDynamicValue(ref25);
            TESTC("setq_without_context", LReference(SS).Evaluate(), ref25);
            LContextRef pcont(new LExpressionContext);
            LReference unbound;
            TESTB("no_lex_binding", pcont->GetBinding(SS.GetPtr())==0);
            LReference ref36(36);
            pcont->AddBinding(SS.GetPtr(), ref36);
            SReference *pref;
            pref = pcont->GetBinding(SS.GetPtr());
            TESTC("lex_binding_playback", *pref, ref36);
            TESTC("dynamic_value_unaffected", LReference(SS).Evaluate(), ref25);

            LContextRef emptycontext(new LExpressionContext);
            LispContinuation cont;
            cont.SetContext(emptycontext);
            TESTC("dynamic_value_when_no_lex",
                  LReference(SS).Evaluate(cont), ref25);

            cont.SetContext(pcont);
            TESTC("lexical_value", LReference(SS).Evaluate(cont), ref36);
            TEST("lexical_value_txt", LReference(SS).Evaluate(cont)->
                 TextRepresentation().c_str(),
                 "36");
            // Now create an inner lexical context
            LContextRef pcont2(new LExpressionContext(pcont));
            // And evaluate within it
            cont.SetContext(pcont2);
            TESTC("lexical_value_in_inner_context",
                  LReference(SS).Evaluate(cont), ref36);

#if 0
            // Now lets change the value of SS
            LReference ref49(49);
            SS->Setq(ref49, pcont2);
            // We've just changed the EXISTING binding
            TESTC("inner_setq_changes_same_binding", *pref, ref49);
            // Let's shadow that binding by creating a new one
            LReference ref64(64);
            pcont2->AddLexicalBinding(SS.GetPtr(), ref64);
            // Now we test that it is in effect, 
            //   then return to the outer context
            //   and see THAT binding is still unchanged
            TESTC("shadow_lexical_binding",
                  LReference(SS).Evaluate(pcont2), ref64);
            TESTC("shadowed_binding_untouched",
                  LReference(SS).Evaluate(pcont), ref49);
#endif
        }
        poc();
        TestSubsection("Dynamic_binding");
        {
            LSymbol SS("SS");
            LSymbol DD("DD");
            TESTB("symbol_is_lexical_by_default", !(SS->IsDynamic()));
            SS->SetDynamicBinding();
            TESTB("switch_to_dynamic_binding", SS->IsDynamic());
            SS->SetDynamicBinding(false);
            TESTB("switch_back_to_lexical", !(SS->IsDynamic()));
            // Lets make DD dynamic
            DD->SetDynamicBinding();
            // Let's assign a dynamic value to both symbols
            LReference ref25(25);
            SS->SetDynamicValue(ref25);
            DD->SetDynamicValue(ref25);
            // Now let's make a lexical context
            LContextRef pcont(new LExpressionContext);
            // We try to bind both symbols lexically,
            // and for DD that must have no visible effect
            //         (unless we restore lexical binding style for it)
            LReference ref36(36);
            pcont->AddBinding(SS.GetPtr(), ref36);
            pcont->AddBinding(DD.GetPtr(), ref36);
            LispContinuation cont;
            cont.SetContext(pcont);
            TESTC("lex_symbol_obeys_lex_binding",
                  LReference(SS).Evaluate(cont), ref36);
            TESTC("dyn_symbol_ignores_lex_binding",
                  LReference(DD).Evaluate(cont), ref25);
#if 0
            // Now we Setq the both symbols and see that SS'dynamic value
            //   is still untouched while DD's changed
            LReference ref49(49);
            SS->Setq(ref49, pcont);
            DD->Setq(ref49, pcont);
            // They now both evaluate to ref49 within the context
            TESTC("lex_symbol_setq", LReference(SS).Evaluate(pcont), ref49);
            TESTC("dyn_symbol_setq", LReference(DD).Evaluate(pcont), ref49);
            // ... but for SS it is lexical value while for DD is dynamic
            TESTC("lex_symbol_lex", SS->GetDynamicValue(), ref25);
            TESTC("dyn_symbol_dyn", DD->GetDynamicValue(), ref49);
            // Lets now return to the top-level and check if everything's Ok
            LContextRef topcont(new LExpressionContext);
            TESTC("lex_symbol_value_survived",
                  LReference(SS).Evaluate(topcont), ref25);
            TESTC("dyn_symbol_value_changed",
                  LReference(DD).Evaluate(topcont), ref49);
#endif
        }
        poc();
        TestSubsection("Apply");
        {
            SListConstructor L;
            LReference plus = new LFunctionPlus;
            LReference res = plus.Apply((L|1,2,3));
            TESTTR("apply", res, "6");
        }
        TestSubsection("Form_evaluation");
        {
            SListConstructor L;
            LSymbol A("A");
            LReference frm = (L|QUOTE, A);
            LReference res = frm.Evaluate();
            LReference must_be = A;
            TEST("evaluate_quote", res->TextRepresentation().c_str(),
                 must_be->TextRepresentation().c_str());
            frm = (L|QUOTE,(L|1, 2, 3));
            TEST("evaluate_quoted_list",
                 frm.Evaluate()->TextRepresentation().c_str(),
                 "(1 2 3)");
        }
        TestSubsection("UserLispFunctions");
        {
            LListConstructor L;
            LSymbol f("f");
            f->SetFunction(square);
            LReference fref = (L| f, 15);
            TEST("user_function", fref.Evaluate().GetInt(), 225);
            LReference fref2 = (L| square, 25);
            try {
                TEST("user_function_by_value", fref2.Evaluate().GetInt(), 625);
            }
            catch(...) {
                TESTB("user_function_by_value", false);
            }
        }
        TestScore();
        poc();
    }
    catch(IntelibX &x) {
        printf("\nCaught IntelibX: %s\n", x.Description() );
        if(x.Parameter().GetPtr()) {
            printf("%s\n", x.Parameter()->TextRepresentation().c_str());
        }
    }
    catch(...) {
        printf("Something strange caught\n");
    }
    poc();
    return 0;
}
Exemple #2
0
int main() {
    poc();
    f(list(1, 2, 3, 4, 5));
}
Exemple #3
0
int main()
{
    poc();
    try {
        TestSection("SVector");
        TestSubsection("Matrix");
        {
            SMatrix<5> matrix;
            matrix[1][2][1][0][1] = 123;
            matrix[2][1][0][1][4] = 777;

            TEST("matrix_1", matrix[1][2][1][0][1].GetInt(), 123);
            TEST("matrix_2", matrix[2][1][0][1][4].GetInt(), 777);
        }
        TestSubsection("Matrix_has_you");
        {
            SVector m;
            SVector m0;
            SVector m1;
            SVector m00;
            SVector m01;
            SVector m10;
            SVector m11;

            m10[1] = SReference(25);

            m0[0] = m00;
            m0[1] = m01;
            m1[0] = m10;
            m1[1] = m11;

            m[0]  = m0;
            m[1]  = m1;

            SMatrixRef<3> matr(m);

            TEST("matrix_has_you", matr[1][0][1].GetInt(), 25);
        }
        TestSubsection("resize");
        {
            SVector v;
            v[7] = 5;
            TEST("resize_1", v->Size(), 8);
            v[12] = 5;
            TEST("resize_2", v->Size(), 13);
            v[13] = 5;
            TEST("resize_3", v->Size(), 14);
        }
#if INTELIB_DEBUG_COUNTERS == 1
        TestSubsection("leaks");
        {
            int before = SExpression::object_counter;
            {
                SVector v;
                v[7] = 5;
                SVectorRef vr(v);
                SVectorRef vr2;
                vr2 = vr;
                SVectorRef vr3(vr2);
                for(int i=0; i<200; i++) vr3[vr3->Size()] = SReference(i);
            }
            TEST("no_leaks", SExpression::object_counter, before);
        }
#endif
        TestSubsection("TextRepresentation");
        {
            SVector v;
            for(int i=0; i<5; i++) v[i]=i;
            TEST("text_rep", v->TextRepresentation().c_str(),
                 "#~(0 1 2 3 4)");

        }
        TestSubsection("Range Copying");
        {
            SVector v;
            for(int i=0; i<15; i++) v[i]=i;

            SVectorRange r(v,5,3);
            TEST("range_copy", r.Copy()->TextRepresentation().c_str(),
                 "#~(5 6 7)");
            TESTB("copy_keeps_positive_resizeability",
                 r.Copy()->IsResizeable());

            SVector vn(5);
            for(int i=0; i<5; i++) vn[i]=i*100;
            SVectorRange rn(vn,1,2);
            TESTB("copy_keeps_negative_resizeability",
                 !rn.Copy()->IsResizeable());
            TESTB("copy_positive_resizeability",
                 rn.Copy(true)->IsResizeable());
            TESTB("copy_negative_resizeability",
                 !rn.Copy(false)->IsResizeable());

            SVectorRange r200(v,5,200);
            TEST("range_size_limited", r200.Copy()->Size(), 10);

        }
        TestSubsection("Range Erasing");
        {
            SVector v;
            for(int i=0; i<15; i++) v[i]=i;
            SVectorRange r(v,3,10);
            r.Erase();
            TEST("range_erase", v->TextRepresentation().c_str(),
                 "#~(0 1 2 13 14)");
            TEST("range_erase_size", v->Size(), 5);
            TEST("range_erase_range_len", r.Copy()->Size(), 0);

        }
        TestSubsection("Range Replacing");
        {
            SVector v, w;
            for(int i=0; i<15; i++) v[i]=i;
            for(int i=0; i<5; i++) w[i]=i*100;

            SVectorRange r(v,3,10);
            r.Replace(w);
            TEST("range_replace_less", v->TextRepresentation().c_str(),
                 "#~(0 1 2 0 100 200 300 400 13 14)");
            TEST("range_replace_less_size", v->Size(), 10);
            TEST("range_replace_less_range_len", r.Copy()->Size(), 5);
        }
        {
            SVector v, w;
            for(int i=0; i<10; i++) v[i]=i;
            for(int i=0; i<5; i++) w[i]=i*100;

            SVectorRange r(w,1,2);
            r.Replace(v);
            TEST("range_replace_more", w->TextRepresentation().c_str(),
                 "#~(0 0 1 2 3 4 5 6 7 8 9 300 400)");
            TEST("range_replace_more_size", w->Size(), 13);
            TEST("range_replace_more_range_len", r.Copy()->Size(), 10);
        }
        TestScore();
    }
    catch(const IntelibX &ex) {
        printf("Caught IntelibX: %s\n%s\n",
            ex.Description(),
            ex.Parameter().GetPtr() ?
                ex.Parameter()->TextRepresentation().c_str() : "");
    }
    catch(...) {
        printf("Something strange caught\n");
    }
    poc();
    return 0;
}
Exemple #4
0
int main()
{
    poc();
#ifdef INTELIB_DEBUG_COUNTERS
    int before = SExpression::object_counter;
#endif
    try {
        TestSection("RefalExpressions");
        TestSubsection("TextRepresentation");
        {
            RfReference empty(new RfExpression);
            TESTTR("empty_rfexpr", empty, "");

            TESTTR("empty_subexpr", R|~R, "()");
        }
        TestSubsection("Construction");
        {
            TESTTR("single", (R|99), "99");
            TESTTR("plain", (R|1,2,3), "1 2 3");
            TESTTR("not_plain", (R|1,(R|2), (R|3,4,(R|5,6),7),8),
                   "1 (2) (3 4 (5 6) 7) 8");
            TESTTR("with_call", (R|1, R<2, 3, 4>R, 5), "1 <2 3 4> 5");
            TESTTR("just_a_call", (R<1,2,3,4>R), "<1 2 3 4>");
            TESTTR("just_a_call_1", (R<1>R), "<1>");
            TESTTR("just_a_call_2", (R<1,2>R), "<1 2>");
            TESTTR("call_in_call", (R<1,2, R<3,4,R<5,6,7>R,8>R,9>R),
                   "<1 2 <3 4 <5 6 7> 8> 9>");

            TESTTR("multy_within_expr", (R|1,2,R<1,R<2,R<3,4>R>R>R,5),
                   "1 2 <1 <2 <3 4>>> 5");
        }
        TestSubsection("MultyCallConstructor");
        {
          #if 0
            TESTTR("x", (R<R<1), "<#<#1");
            TESTTR("xx", (R<R<1, 2), "<#<#1 2");
            TESTTR("xxx", (R<R<1, 2>R), "<#<1 2>");
          #endif
            TESTTR("call_in_beginning_of_call", (R<R<1,2>R, 3>R),
                   "<<1 2> 3>");
            TESTTR("call_in_beginning_of_call_noclose", (R<R<1,2>R, 3),
                   "<#<1 2> 3");
            TESTTR("call_in_beginning_of_call2", (R<R<R<1,2>R, 3>R,4>R),
                   "<<<1 2> 3> 4>");
            TESTTR("call_in_beginning_of_call3", (R<R<R<1,2>R,3>R,4>R),
                   "<<<1 2> 3> 4>");


            TESTTR("multy_within_expr", (R|1,2,R<R<R<3,4>R>R>R,5),
                   "1 2 <<<3 4>>> 5");

            TESTTR("call_starts_with_subexpr",
                   (R|0,R<R<(R|1,2,3),4>R>R,5),
                   "0 <<(1 2 3) 4>> 5");

            TESTTR("inner_call_of_one_symbol",
                   (R<R<1>R>R), "<<1>>");

            TESTTR("inner_call_of_one_symbol",
                   (R|0,R<R<1>R>R,2), "0 <<1>> 2");

        }
        TestSubsection("Strings decomposition");
        {
            TESTTR("simple_char", (R|"a"), "\"a\"");
            TESTTR("char_shuffled", (R|1,"a",2), "1 \"a\" 2");
            TESTTR("char_shuffled2", (R|"a",2,"b"),
                "\"a\" 2 \"b\"");
            TESTTR("char_shuffled3", (R|"a",2,3,"b"), "\"a\" 2 3 \"b\"");
            TESTTR("char_shuffled4", (R|"a",2,3,"bc"),
                "\"a\" 2 3 \"bc\"");
            TESTTR("char_shuffled4", (R|"ab",2,3,"bc"),
                "\"ab\" 2 3 \"bc\"");
            TESTTR("char_shuffled4", (R|"ab",(R|2,3),"bc"),
                   "\"ab\" (2 3) \"bc\"");
            TESTTR("simple_strings", (R|"abcd","efgh","ijkl"),
                   "\"abcdefghijkl\"");
            TESTTR("strings_in_a_call", (R<"abcd","efgh","ijkl">R),
                   "<\"abcdefghijkl\">");
            TESTTR("one_strings_in_many_calls", (R<R<R<"abcd">R>R>R),
                   "<<<\"abcd\">>>");
        }
        TestSubsection("Pairing");
        {
            RfReference r((R|1,(R|2,(R|3,(R|4),5),6),7));
            RfListItem *temp1 = r->GetFirst(), *temp2 = r->GetLast();
            temp1 = temp1->next; temp2 = temp2->prev;
            for (int i=0; i<2; i++) {
                TESTB("paired_lr",
                     temp1->pair == 
                     temp2);
                TESTB("paired_rl",
                     temp2->pair == 
                     temp1);
                temp1 = temp1->next; temp2 = temp2->prev;
                temp1 = temp1->next; temp2 = temp2->prev;
            }

        }
        TestSubsection("ConvertToLisp");
        {
            RfReference r((R|1,(R|"abcd",(R|3,(R|4),"efgh"),6),7,"i"));
            SReference r1 = r->ConvertToLisp();
            TESTTR("convert_to_lisp", r1,
                   "(1 (\"abcd\" (3 (4) \"efgh\") 6) 7 \"i\")");

        }
        TestSubsection("Memory leaks");
        {
#ifdef INTELIB_DEBUG_COUNTERS
            TEST("no_leaks", SExpression::object_counter, before);
#endif
        }
        TestScore();
    }
    catch(const IntelibX &ex)
    {
        printf("Caught IntelibX: %s\n", ex.Description());
        doprint(ex.Parameter().GetPtr());
        printf("\n");
    }
    catch(...)
    {
        printf("Something strange caught\n");
    }
    poc();
    return 0;
}
Exemple #5
0
int main()
{
    poc();
    SLabel A("A"); SExpressionLabel *pa = A.GetPtr();
    SLabel B("B"); SExpressionLabel *pb = B.GetPtr();
    SLabel C("C"); SExpressionLabel *pc = C.GetPtr();
    SLabel D("D"); SExpressionLabel *pd = D.GetPtr();
    SLabel E("E"); SExpressionLabel *pe = E.GetPtr();
    SLabel F("F"); SExpressionLabel *pf = F.GetPtr();
    SExpressionLabel *pall[] = { pa, pb, pc, pd, pe, pf };
    try {
        TestSection("IntelibBindingsSet");
        TestSubsection("Creation");
        {
            IntelibBindingsSet t;
            TESTB("empty0", !t.GetBinding(pa));
            TESTB("empty25", !t.GetBinding(pb));
            *(t.AddBinding(pb)) = SReference(2525);
            TESTTR("root_only", *(t.GetBinding(pb)), "2525");
            *(t.AddBinding(pc)) = SReference(3636);
            TESTB("root_still_there", t.GetBinding(pb));
            TESTTR("root_still_has_value", *(t.GetBinding(pb)), "2525");
            TESTB("second_val_exists", t.GetBinding(pc));
            TESTTR("second_val", *(t.GetBinding(pc)), "3636");
            TESTB("root_still_there", t.GetBinding(pb));
        }
        TestSubsection("Allocation");
        {
            IntelibBindingsSet t;
            SReference* loc[4];
            for(int i = 0; i < 4; i++) {
                loc[i] = t.AddBinding(pall[i]);
                *loc[i] = (i+1) * 1000;
            }
            t.AddBinding(pf);
            SReference *l1 = t.GetBinding(pb);
            TESTB("still_there_after_resize", l1);
            TESTTR("still_value_after_resize", *l1, "2000");
            SReference *l0 = t.GetBinding(pa);
            TESTB("root_there_after_resize", l0);
            TESTTR("root_value_after_resize", *l0, "1000");
            //TESTB("resize_really_done", l1 != loc[1]);
        }
        TestSubsection("Iterator");
        {
            IntelibBindingsSet t;
            IntelibBindingsSet::Iterator iter0(t);
            SReference ref;
            const SExpressionLabel *key;
            TESTB("iterator_on_empty", !iter0.GetNext(key, ref));
            *(t.AddBinding(pa)) = SReference(2);
            *(t.AddBinding(pb)) = SReference(20);
            *(t.AddBinding(pc)) = SReference(200);
            IntelibBindingsSet::Iterator iter1(t);
            int sum2 = 0;
            while(iter1.GetNext(key, ref)) {
                if(ref.GetPtr()) sum2 += ref.GetInt();
            }
            TEST("iterator_values", sum2, 222);
        }
        TestSubsection("LotsOfBindings");
        {
            IntelibBindingsSet t;
            *(t.AddBinding(pa)) = SReference(1);
            *(t.AddBinding(pb)) = SReference(2);
            *(t.AddBinding(pc)) = SReference(3);
            *(t.AddBinding(pd)) = SReference(4);
            *(t.AddBinding(pe)) = SReference(5);
            *(t.AddBinding(pf)) = SReference(6);
            TESTB("value_there_1", t.GetBinding(pa));
            TESTB("value_there_2", t.GetBinding(pb));
            TESTB("value_there_3", t.GetBinding(pc));
            TESTB("value_there_4", t.GetBinding(pd));
            TESTB("value_there_5", t.GetBinding(pe));
            TESTB("value_there_6", t.GetBinding(pf));
        }
        TestScore();
    }
    catch(const IntelibX &ex) {
        printf("Caught IntelibX: %s\n%s\n",
            ex.Description(),
            ex.Parameter().GetPtr() ? 
                ex.Parameter()->TextRepresentation().c_str()
                : ""
        );
    }
    catch(...) {
        printf("Something strange caught\n");
    }
    poc();
    return 0;
}