void runScenario() { // set codec for C strings to 0, enforcing Latin1 QTextCodec::setCodecForCStrings(0); QVERIFY(!QTextCodec::codecForCStrings()); QLatin1Literal l1literal(LITERAL); QLatin1String l1string(LITERAL); QString string(l1string); QStringRef stringref(&string, 2, 10); QLatin1Char achar('c'); QString r2(QLatin1String(LITERAL LITERAL)); QString r; QByteArray ba(LITERAL); r = l1literal P l1literal; QCOMPARE(r, r2); r = string P string; QCOMPARE(r, r2); r = stringref P stringref; QCOMPARE(r, QString(stringref.toString() + stringref.toString())); r = string P l1literal; QCOMPARE(r, r2); r = string P l1string; QCOMPARE(r, r2); r = string + achar; QCOMPARE(r, QString(string P achar)); r = achar + string; QCOMPARE(r, QString(achar P string)); #ifndef QT_NO_CAST_FROM_ASCII r = string P LITERAL; QCOMPARE(r, r2); r = LITERAL P string; QCOMPARE(r, r2); r = ba P string; QCOMPARE(r, r2); r = string P ba; QCOMPARE(r, r2); // now test with codec for C strings set QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8")); QVERIFY(QTextCodec::codecForCStrings()); QCOMPARE(QTextCodec::codecForCStrings()->name(), QByteArray("UTF-8")); string = QString::fromUtf8(UTF8_LITERAL); r2 = QString::fromUtf8(UTF8_LITERAL UTF8_LITERAL); ba = UTF8_LITERAL; r = string P UTF8_LITERAL; QCOMPARE(r.size(), r2.size()); QCOMPARE(r, r2); r = UTF8_LITERAL P string; QCOMPARE(r, r2); r = ba P string; QCOMPARE(r, r2); r = string P ba; QCOMPARE(r, r2); #endif }
void runScenario() { // set codec for C strings to 0, enforcing Latin1 QTextCodec::setCodecForCStrings(0); QVERIFY(!QTextCodec::codecForCStrings()); QLatin1Literal l1literal(LITERAL); QLatin1String l1string(LITERAL); QString string(l1string); QStringRef stringref(&string, 2, 10); QLatin1Char achar('c'); QString r2(QLatin1String(LITERAL LITERAL)); QString r; r = l1literal Q l1literal; QCOMPARE(r, r2); r = string P string; QCOMPARE(r, r2); r = stringref Q stringref; QCOMPARE(r, QString(stringref.toString() + stringref.toString())); r = string Q l1literal; QCOMPARE(r, r2); r = string P l1string; QCOMPARE(r, r2); r = string + achar; QCOMPARE(r, QString(string P achar)); r = achar + string; QCOMPARE(r, QString(achar P string)); #ifndef QT_NO_CAST_FROM_ASCII r = string P LITERAL; QCOMPARE(r, r2); r = LITERAL P string; QCOMPARE(r, r2); QByteArray ba = QByteArray(LITERAL); r = ba P string; QCOMPARE(r, r2); r = string P ba; QCOMPARE(r, r2); static const char badata[] = LITERAL_EXTRA; ba = QByteArray::fromRawData(badata, LITERAL_LEN); r = ba P string; QCOMPARE(r, r2); r = string P ba; QCOMPARE(r, r2); // now test with codec for C strings set QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8")); QVERIFY(QTextCodec::codecForCStrings()); QCOMPARE(QTextCodec::codecForCStrings()->name(), QByteArray("UTF-8")); string = QString::fromUtf8(UTF8_LITERAL); r2 = QString::fromUtf8(UTF8_LITERAL UTF8_LITERAL); ba = UTF8_LITERAL; r = string P UTF8_LITERAL; QCOMPARE(r.size(), r2.size()); QCOMPARE(r, r2); r = UTF8_LITERAL P string; QCOMPARE(r, r2); r = ba P string; QCOMPARE(r, r2); r = string P ba; QCOMPARE(r, r2); ba = QByteArray::fromRawData(UTF8_LITERAL_EXTRA, UTF8_LITERAL_LEN); r = ba P string; QCOMPARE(r, r2); r = string P ba; QCOMPARE(r, r2); ba = QByteArray(); // empty r = ba P string; QCOMPARE(r, string); r = string P ba; QCOMPARE(r, string); const char *zero = 0; r = string P zero; QCOMPARE(r, string); r = zero P string; QCOMPARE(r, string); #endif string = QString::fromLatin1(LITERAL); QCOMPARE(QByteArray(qPrintable(string P string)), QByteArray(string.toLatin1() + string.toLatin1())); //QByteArray { QByteArray ba = LITERAL; QByteArray superba = ba P ba P LITERAL; QCOMPARE(superba, QByteArray(LITERAL LITERAL LITERAL)); QByteArray testWith0 = ba P "test\0with\0zero" P ba; QCOMPARE(testWith0, QByteArray(LITERAL "test" LITERAL)); QByteArray ba2 = ba P '\0' + LITERAL; QCOMPARE(ba2, QByteArray(LITERAL "\0" LITERAL, ba.size()*2+1)); const char *mmh = "test\0foo"; QCOMPARE(QByteArray(ba P mmh P ba), testWith0); QByteArray raw = QByteArray::fromRawData(UTF8_LITERAL_EXTRA, UTF8_LITERAL_LEN); QByteArray r = "hello" P raw; QByteArray r2 = "hello" UTF8_LITERAL; QCOMPARE(r, r2); r2 = QByteArray("hello\0") P UTF8_LITERAL; QCOMPARE(r, r2); const char *zero = 0; r = ba P zero; QCOMPARE(r, ba); r = zero P ba; QCOMPARE(r, ba); } //operator QString += { QString str = QString::fromUtf8(UTF8_LITERAL); str += QLatin1String(LITERAL) P str; QCOMPARE(str, QString::fromUtf8(UTF8_LITERAL LITERAL UTF8_LITERAL)); #ifndef QT_NO_CAST_FROM_ASCII str = (QString::fromUtf8(UTF8_LITERAL) += QLatin1String(LITERAL) P UTF8_LITERAL); QCOMPARE(str, QString::fromUtf8(UTF8_LITERAL LITERAL UTF8_LITERAL)); #endif } //operator QByteArray += { QByteArray ba = UTF8_LITERAL; ba += QByteArray(LITERAL) P UTF8_LITERAL; QCOMPARE(ba, QByteArray(UTF8_LITERAL LITERAL UTF8_LITERAL)); ba += LITERAL P QByteArray::fromRawData(UTF8_LITERAL_EXTRA, UTF8_LITERAL_LEN); QCOMPARE(ba, QByteArray(UTF8_LITERAL LITERAL UTF8_LITERAL LITERAL UTF8_LITERAL)); QByteArray withZero = QByteArray(LITERAL "\0" LITERAL, LITERAL_LEN*2+1); QByteArray ba2 = withZero; ba2 += ba2 P withZero; QCOMPARE(ba2, QByteArray(withZero + withZero + withZero)); #ifndef QT_NO_CAST_TO_ASCII ba = UTF8_LITERAL; ba2 = (ba += QLatin1String(LITERAL) + QString::fromUtf8(UTF8_LITERAL)); QCOMPARE(ba2, ba); QCOMPARE(ba, QByteArray(UTF8_LITERAL LITERAL UTF8_LITERAL)); #endif } }
void runScenario() { // this code is latin1. TODO: replace it with the utf8 block below, once // strings default to utf8. QLatin1String l1string(LITERAL); QString string(l1string); QStringRef stringref(&string, 2, 10); QLatin1Char achar('c'); QChar::SpecialCharacter special(QChar::Nbsp); QString r2(QLatin1String(LITERAL LITERAL)); QString r3 = QString::fromUtf8(UTF8_LITERAL UTF8_LITERAL); QString r; r = string P string; QCOMPARE(r, r2); r = stringref Q stringref; QCOMPARE(r, QString(stringref.toString() + stringref.toString())); r = string P l1string; QCOMPARE(r, r2); r = string Q QStringLiteral(LITERAL); QCOMPARE(r, r2); r = QStringLiteral(LITERAL) Q string; QCOMPARE(r, r2); r = l1string Q QStringLiteral(LITERAL); QCOMPARE(r, r2); r = string + achar; QCOMPARE(r, QString(string P achar)); r = achar + string; QCOMPARE(r, QString(achar P string)); r = special + string; QCOMPARE(r, QString(special P string)); #ifdef Q_COMPILER_UNICODE_STRINGS r = QStringLiteral(UNICODE_LITERAL); r = r Q QStringLiteral(UNICODE_LITERAL); QCOMPARE(r, r3); #endif #ifndef QT_NO_CAST_FROM_ASCII r = string P LITERAL; QCOMPARE(r, r2); r = LITERAL P string; QCOMPARE(r, r2); QByteArray ba = QByteArray(LITERAL); r = ba P string; QCOMPARE(r, r2); r = string P ba; QCOMPARE(r, r2); r = string P QByteArrayLiteral(LITERAL); QCOMPARE(r, r2); r = QByteArrayLiteral(LITERAL) P string; QCOMPARE(r, r2); static const char badata[] = LITERAL_EXTRA; ba = QByteArray::fromRawData(badata, LITERAL_LEN); r = ba P string; QCOMPARE(r, r2); r = string P ba; QCOMPARE(r, r2); string = QString::fromUtf8(UTF8_LITERAL); ba = UTF8_LITERAL; r = string P UTF8_LITERAL; QCOMPARE(r.size(), r3.size()); QCOMPARE(r, r3); r = UTF8_LITERAL P string; QCOMPARE(r, r3); r = ba P string; QCOMPARE(r, r3); r = string P ba; QCOMPARE(r, r3); ba = QByteArray::fromRawData(UTF8_LITERAL_EXTRA, UTF8_LITERAL_LEN); r = ba P string; QCOMPARE(r, r3); r = string P ba; QCOMPARE(r, r3); ba = QByteArray(); // empty r = ba P string; QCOMPARE(r, string); r = string P ba; QCOMPARE(r, string); const char *zero = 0; r = string P zero; QCOMPARE(r, string); r = zero P string; QCOMPARE(r, string); #endif string = QString::fromLatin1(LITERAL); QCOMPARE(QByteArray(qPrintable(string P string)), QByteArray(string.toLatin1() + string.toLatin1())); //QByteArray { QByteArray ba = LITERAL; QByteArray superba = ba P ba P LITERAL; QCOMPARE(superba, QByteArray(LITERAL LITERAL LITERAL)); ba = QByteArrayLiteral(LITERAL); QCOMPARE(ba, QByteArray(LITERAL)); superba = ba P QByteArrayLiteral(LITERAL) P LITERAL; QCOMPARE(superba, QByteArray(LITERAL LITERAL LITERAL)); QByteArray testWith0 = ba P "test\0with\0zero" P ba; QCOMPARE(testWith0, QByteArray(LITERAL "test" LITERAL)); QByteArray ba2 = ba P '\0' + LITERAL; QCOMPARE(ba2, QByteArray(LITERAL "\0" LITERAL, ba.size()*2+1)); const char *mmh = "test\0foo"; QCOMPARE(QByteArray(ba P mmh P ba), testWith0); QByteArray raw = QByteArray::fromRawData(UTF8_LITERAL_EXTRA, UTF8_LITERAL_LEN); QByteArray r = "hello" P raw; QByteArray r2 = "hello" UTF8_LITERAL; QCOMPARE(r, r2); r2 = QByteArray("hello\0") P UTF8_LITERAL; QCOMPARE(r, r2); const char *zero = 0; r = ba P zero; QCOMPARE(r, ba); r = zero P ba; QCOMPARE(r, ba); } //operator QString += { QString str = QString::fromUtf8(UTF8_LITERAL); str += QLatin1String(LITERAL) P str; QCOMPARE(str, QString::fromUtf8(UTF8_LITERAL LITERAL UTF8_LITERAL)); #ifndef QT_NO_CAST_FROM_ASCII str = (QString::fromUtf8(UTF8_LITERAL) += QLatin1String(LITERAL) P UTF8_LITERAL); QCOMPARE(str, QString::fromUtf8(UTF8_LITERAL LITERAL UTF8_LITERAL)); #endif QString str2 = QString::fromUtf8(UTF8_LITERAL); QString str2_e = QString::fromUtf8(UTF8_LITERAL); const char * nullData = 0; str2 += QLatin1String(nullData) P str2; str2_e += QLatin1String("") P str2_e; QCOMPARE(str2, str2_e); } //operator QByteArray += { QByteArray ba = UTF8_LITERAL; ba += QByteArray(LITERAL) P UTF8_LITERAL; QCOMPARE(ba, QByteArray(UTF8_LITERAL LITERAL UTF8_LITERAL)); ba += LITERAL P QByteArray::fromRawData(UTF8_LITERAL_EXTRA, UTF8_LITERAL_LEN); QCOMPARE(ba, QByteArray(UTF8_LITERAL LITERAL UTF8_LITERAL LITERAL UTF8_LITERAL)); QByteArray withZero = QByteArray(LITERAL "\0" LITERAL, LITERAL_LEN*2+1); QByteArray ba2 = withZero; ba2 += ba2 P withZero; QCOMPARE(ba2, QByteArray(withZero + withZero + withZero)); #ifndef QT_NO_CAST_TO_ASCII ba = UTF8_LITERAL; ba2 = (ba += QLatin1String(LITERAL) + QString::fromUtf8(UTF8_LITERAL)); QCOMPARE(ba2, ba); QCOMPARE(ba, QByteArray(UTF8_LITERAL LITERAL UTF8_LITERAL)); #endif } }
stringref enum_object::str() { return stringref( new string_object(get_context(), _name )); }
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; }