예제 #1
0
bool TestCDecl_Class()
{
	if( strstr(asGetLibraryOptions(), "AS_MAX_PORTABILITY") )
	{
		printf("%s: Skipped due to AS_MAX_PORTABILITY\n", TESTNAME);
		return false;
	}

	bool fail = false;

	asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);

	if( sizeof(Class3) != 12 || sizeof(asvec3_t) != 12 )
	{
		printf("sizeof(Class1) == %d\n", sizeof(Class1));
		printf("sizeof(Class2) == %d\n", sizeof(Class2));
		printf("sizeof(Class3) == %d\n", sizeof(Class3));
		printf("sizeof(asvec3_t) == %d\n", sizeof(asvec3_t));
	}

	// On 64bit Linux these types would be returned in RAX:RDX, and must be informed with asOBJ_APP_CLASS_ALLINTS
	engine->RegisterObjectType("class1", sizeof(Class1), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS | asOBJ_APP_CLASS_ALLINTS);
	engine->RegisterObjectType("class2", sizeof(Class2), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS | asOBJ_APP_CLASS_ALLINTS);
	engine->RegisterObjectType("class3", sizeof(Class3), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS | asOBJ_APP_CLASS_ALLINTS);
	
	engine->RegisterGlobalProperty("class1 c1", &c1);
	engine->RegisterGlobalProperty("class2 c2", &c2);
	engine->RegisterGlobalProperty("class3 c3", &c3);

	engine->RegisterGlobalFunction("class1 _class1()", asFUNCTION(class1), asCALL_CDECL);
	engine->RegisterGlobalFunction("class2 _class2()", asFUNCTION(class2), asCALL_CDECL);
	engine->RegisterGlobalFunction("class3 _class3()", asFUNCTION(class3), asCALL_CDECL);

	COutStream out;

	c1.a = 0;

	engine->SetMessageCallback(asMETHOD(COutStream,Callback), &out, asCALL_THISCALL);
	int r = ExecuteString(engine, "c1 = _class1();");
	if( r < 0 )
	{
		printf("%s: ExecuteString() failed %d\n", TESTNAME, r);
		TEST_FAILED;
	}

	if( c1.a != 0xDEADC0DE )
	{
		printf("%s: Failed to assign object returned from function. c1.a = %X\n", TESTNAME, (unsigned int)c1.a);
		TEST_FAILED;
	}


	c2.a = 0;
	c2.b = 0;

	r = ExecuteString(engine, "c2 = _class2();");
	if( r < 0 )
	{
		printf("%s: ExecuteString() failed %d\n", TESTNAME, r);
		TEST_FAILED;
	}

	if( c2.a != 0xDEADC0DE )
	{
		printf("%s: Failed to assign object returned from function. c2.a = %X\n", TESTNAME, (unsigned int)c2.a);
		TEST_FAILED;
	}

	if( c2.b != 0x01234567 )
	{
		printf("%s: Failed to assign object returned from function. c2.b = %X\n", TESTNAME, (unsigned int)c2.b);
		TEST_FAILED;
	}

	c3.a = 0;
	c3.b = 0;
	c3.c = 0;

	r = ExecuteString(engine, "c3 = _class3();");
	if( r < 0 )
	{
		printf("%s: ExecuteString() failed %d\n", TESTNAME, r);
		TEST_FAILED;
	}

	if( c3.a != 0xDEADC0DE )
	{
		printf("%s: Failed to assign object returned from function. c3.a = %X\n", TESTNAME, (unsigned int)c3.a);
		TEST_FAILED;
	}

	if( c3.b != 0x01234567 )
	{
		printf("%s: Failed to assign object returned from function. c3.b = %X\n", TESTNAME, (unsigned int)c3.b);
		TEST_FAILED;
	}

	if( c3.c != 0x89ABCDEF )
	{
		printf("%s: Failed to assign object returned from function. c3.c = %X\n", TESTNAME, (unsigned int)c3.c);
		TEST_FAILED;
	}

	// Test the vec3 C structure
	// On 64bit Linux this type would be returned in XMM0:XMM1, which is why we need to inform asOBJ_APP_CLASS_ALLFLOATS
	engine->RegisterObjectType("vec3", sizeof(asvec3_t), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS | asOBJ_APP_CLASS_ALLFLOATS);
	engine->RegisterGlobalProperty("vec3 v3", &v3);
	engine->RegisterGlobalFunction("vec3 vec3_123()", asFUNCTION(vec3_123), asCALL_CDECL);

	v3.v[0] = 0;
	v3.v[1] = 0;
	v3.v[2] = 0;
	r = ExecuteString(engine, "v3 = vec3_123();");
	if( r < 0 )
		TEST_FAILED;
	if( v3.v[0] != 1 || v3.v[1] != 2 || v3.v[2] != 3 )
	{
		printf("%s: Got (%f, %f, %f)\n", TESTNAME, v3.v[0], v3.v[1], v3.v[2]);
		TEST_FAILED;
	}

	// Test passing the object types by value to a system function
	r = engine->RegisterGlobalFunction("void class1ByVal(class1)", asFUNCTION(class1ByVal), asCALL_CDECL); assert( r >= 0 );
	r = ExecuteString(engine, "class1 c = _class1(); class1ByVal(c)");
	if( r != asEXECUTION_FINISHED )
		TEST_FAILED;

	r = engine->RegisterGlobalFunction("void class2ByVal(class2)", asFUNCTION(class2ByVal), asCALL_CDECL); assert( r >= 0 );
	r = ExecuteString(engine, "class2 c = _class2(); class2ByVal(c)");
	if( r != asEXECUTION_FINISHED )
		TEST_FAILED;

	Class3 c = class3(); class3ByVal(c);
	r = engine->RegisterGlobalFunction("void class3ByVal(class3)", asFUNCTION(class3ByVal), asCALL_CDECL); assert( r >= 0 );
	r = ExecuteString(engine, "class3 c = _class3(); class3ByVal(c)");
	if( r != asEXECUTION_FINISHED )
		TEST_FAILED;

	engine->Release();

	return fail;
}
static void CreateRepository(CIMRepository & repository)
{
    repository.createNameSpace(NS);

    Array<String> qualifierValue;
    qualifierValue.append("");

    CIMQualifierDecl q1(CIMName ("MappingStrings"), qualifierValue,
        CIMScope::PROPERTY + CIMScope::CLASS);

    // Qualifier name must be "MappingStrings", test the qualifier
    // name is not "MappingStrings"
    CIMQualifierDecl q2(CIMName ("NotMappingStrings"), qualifierValue,
        CIMScope::CLASS);

    repository.setQualifier(NS, q1);
    repository.setQualifier(NS, q2);

    Array<String> classMappingStr;
    classMappingStr.append("OID.IETF | SNMP.1.3.6.1.4.1.892.2.3.9000.8600");

    CIMClass class1(testClass1);
    class1.addQualifier(CIMQualifier(CIMName ("MappingStrings"),
        CIMValue(classMappingStr)));

    Array<String> invalidFormatStr;
    invalidFormatStr.append(
        "Wrong format OID.IETF | SNMP.1.3.6.1.4.1.2.3.9000.8600");
    invalidFormatStr.append("DataType.IETF | OctetString ");

    // create wrong format property mappingStrings value
    class1.addProperty(
        CIMProperty(CIMName("OidDataType"), String("OctetString"))
        .addQualifier(CIMQualifier(CIMName ("MappingStrings"),
            CIMValue(invalidFormatStr))));

    repository.createClass(NS, class1);

    // create invalid mapping string value
    Array<String> class2MappingStr;
    Array<String> mappingStr2;

    class2MappingStr.append("OID.IETF |Invalid Mapping String Value");

    mappingStr2.append("OID.IETF | SNMP.1.3.6.1.4.1.2.3.9000.8600");
    mappingStr2.append("DataType.IETF OctetString ");

    CIMClass class2(testClass2);
    class2.addQualifier(CIMQualifier(CIMName ("MappingStrings"),
        CIMValue(class2MappingStr)));

    class2.addProperty(CIMProperty(CIMName ("OidDataType"), String())
        .addQualifier(CIMQualifier(CIMName ("MappingStrings"),
            CIMValue(mappingStr2))));
    repository.createClass(NS, class2);

    // create non MappingStrings qualifier
    CIMClass class3(testClass3);
    class3.addQualifier(CIMQualifier(CIMName ("NotMappingStrings"),
        CIMValue(classMappingStr)));

    repository.createClass(NS, class3);

    // error building ASN.1 representation
    Array<String> class4MappingStr;
    class4MappingStr.append("OID.IETF | SNMP.1.204.6.1.6.3.1.330.5.1.0 ");

    CIMClass class4(testClass4);
    class4.addQualifier(CIMQualifier(CIMName ("MappingStrings"),
        CIMValue(class4MappingStr)));

    repository.createClass(NS, class4);

    // create incorrect class mappingStrings value
    Array<String> class5MappingStr;
    class5MappingStr.append("OID.IETF | SNMP.1.3.6.1.6.test.1.1.5.1.3 ");

    CIMClass class5(testClass5);
    class5.addQualifier(CIMQualifier(CIMName
        ("MappingStrings"), CIMValue(class5MappingStr)));

    // create incorrect property name
    class5.addProperty(
        CIMProperty(CIMName ("WrongPropertyName"), String("OctetString"))
            .addQualifier(CIMQualifier(CIMName ("MappingStrings"),
                CIMValue(class5MappingStr))));

    repository.createClass(NS, class5);

    // create incorrect property mappingStrings value
    Array<String> class6MappingStr;
    class6MappingStr.append("OID.IETF | SNMP.1.3.6.1.6.3.1.1.0.1 ");

    Array<String> mappingStr6;
    mappingStr6.append("OID.IETF | SNMP.1.3.6.1.6.test.1.1.5.1.3");
    mappingStr6.append("DataType.IETF | OctetString");

    CIMClass class6(testClass6);
    class6.addQualifier(CIMQualifier(CIMName ("MappingStrings"),
        CIMValue(class6MappingStr)));
    class6.addProperty(
        CIMProperty(CIMName ("OidDataType"), String("OctetString"))
            .addQualifier(CIMQualifier(CIMName ("MappingStrings"),
                CIMValue(mappingStr6))));

    repository.createClass(NS, class6);

    // create unsupportted SNMP Data Type for the CIM property
    Array<String> class7MappingStr;
    class7MappingStr.append("OID.IETF | SNMP.1.3.6.1.6.3.1.1.5.1 ");

    Array<String> mappingStr7;
    mappingStr7.append("OID.IETF | SNMP.1.3.6.1.6.test.1.1.5.1.3");
    mappingStr7.append("DataType.IETF | test ");

    CIMClass class7(testClass7);
    class7.addQualifier(CIMQualifier(CIMName ("MappingStrings"),
        CIMValue(class7MappingStr)));
    class7.addProperty(
        CIMProperty(CIMName ("OidDataType"), String("test"))
            .addQualifier(CIMQualifier(CIMName ("MappingStrings"),
                CIMValue(mappingStr7))));

    repository.createClass(NS, class7);

    // create invalid syntax for MappingStrings qualifier
    Array<String> invalidSyntax;
    Array<String> class8MappingStr;
    class8MappingStr.append("OID.IETF Invalid Syntax for MappingStrings");

    Array<String> mappingStr8;
    mappingStr8.append("OID.IETF | SNMP.1.3.6.1.4.1.2.3.9000.8600");
    mappingStr8.append("DataType.IETF | OctetString ");

    CIMClass class8(testClass8);
    class8.addQualifier(CIMQualifier(CIMName ("MappingStrings"),
        CIMValue(class8MappingStr)));

    class8.addProperty(CIMProperty(CIMName ("OidDataType"), String())
        .addQualifier(CIMQualifier(CIMName ("MappingStrings"),
            CIMValue(mappingStr8))));
    repository.createClass(NS, class8);
}