TEST(Expression_Map, mapMerge)
{
	//map1: {1|->2,3|->4,6|->7}
	TVP map1 = createMap1();
	//map2: {7|->8, 9|->11}
	TVP map2 = createMap3();

	// Create a set of maps
	TVP arr[2] = {map1, map2};
	TVP set = newSetWithValues(2, arr);

	//map1: {1|->2,3|->4,6|->7}
	TVP map = createMap1();

	TVP map_res = vdmMapMerge(set);

	// Get domain
	TVP map_dom = vdmMapDom(map_res);

	TVP res = vdmSetMemberOf(map_dom,newInt(3));
	EXPECT_EQ(true, res->value.boolVal);

	res = vdmSetMemberOf(map_dom,newInt(9));
	EXPECT_EQ(true, res->value.boolVal);
}
TEST(Expression_SetRandom, setUnion)
{
	const int numelems1 = 101;
	const int numelems2 = 97;
	TVP randelems1[numelems1];
	TVP randelems2[numelems2];
	TVP set1;
	TVP set2;
	TVP unionset;
	TVP res;

	//Generate the random test value collections.
	for(int i = 0; i < numelems1; i++)
	{
		randelems1[i] = newInt(rand());
	}

	for(int i = 0; i < numelems2; i++)
	{
		randelems2[i] = newInt(rand());
	}

	//Create the random test sets.
	set1 = newSetWithValues(numelems1, randelems1);
	set2 = newSetWithValues(numelems2, randelems2);
	unionset = vdmSetUnion(set1, set2);

	//Check membership of each test element in union.
	for(int i = 0; i < numelems1; i++)
	{
		res = vdmSetMemberOf(unionset, randelems1[i]);
		EXPECT_EQ(true, res->value.boolVal);
		vdmFree(res);
	}
	for(int i = 0; i < numelems2; i++)
	{
		res = vdmSetMemberOf(unionset, randelems2[i]);
		EXPECT_EQ(true, res->value.boolVal);
		vdmFree(res);
	}

	//Ensure that there are no other elements.
	EXPECT_EQ(true, ((struct Collection*)(unionset->value.ptr))->size <= \
				((struct Collection*)(set1->value.ptr))->size + \
				((struct Collection*)(set2->value.ptr))->size);

	//Wrap up.
	vdmFree(set1);
	vdmFree(set2);
	vdmFree(unionset);
	for(int i = 0; i < numelems1; i++)
	{
		vdmFree(randelems1[i]);
	}
	for(int i = 0; i < numelems2; i++)
	{
		vdmFree(randelems2[i]);
	}
}
TEST(Expression_SetRandom, setIntersection)
{
	const int numelems1 = 101;
	const int numelems2 = 97;
	TVP randelems1[numelems1];
	TVP randelems2[numelems2];
	TVP set1;
	TVP set2;
	TVP interset;
	TVP res;

	//Generate the random test value collections.  Get random values modulo 100
	//to overcome the stellar performance of the random number generator.
	srand(time(0));
	for(int i = 0; i < numelems1; i++)
	{
		randelems1[i] = newInt(rand() % 100);
	}

	for(int i = 0; i < numelems2; i++)
	{
		randelems2[i] = newInt(rand() % 100);
	}

	//Create the random test sets.
	set1 = newSetWithValues(numelems1, randelems1);
	set2 = newSetWithValues(numelems2, randelems2);
	interset = vdmSetInter(set1, set2);
	UNWRAP_COLLECTION(intercol, interset);

	//Check that intersection is a subset of both sets.
	res = vdmSetSubset(interset, set1);
	EXPECT_EQ(true, res->value.boolVal);
	vdmFree(res);
	res = vdmSetSubset(interset, set2);
	EXPECT_EQ(true, res->value.boolVal);
	vdmFree(res);

	//Ensure that intersection has at most as many elements as the smallest of the two sets.
	UNWRAP_COLLECTION(col1, set1);
	UNWRAP_COLLECTION(col2, set2);
	EXPECT_EQ(true, (intercol->size <= col1->size) && (intercol->size <= col2->size));

	//Wrap up.
	vdmFree(set1);
	vdmFree(set2);
	vdmFree(interset);
	for(int i = 0; i < numelems1; i++)
	{
		vdmFree(randelems1[i]);
	}
	for(int i = 0; i < numelems2; i++)
	{
		vdmFree(randelems2[i]);
	}
}
TEST(Expression_SetRandom, setDifference)
{
	const int numelems1 = 101;
	const int numelems2 = 97;
	TVP randelems1[numelems1];
	TVP randelems2[numelems2];
	TVP set1;
	TVP set2;
	TVP diffset;
	TVP interset;
	TVP res;

	//Generate the random test value collections.  Get random values modulo 100
	//to overcome the stellar performance of the random number generator.
	srand(time(0));
	for(int i = 0; i < numelems1; i++)
	{
		randelems1[i] = newInt(rand() % 100);
	}

	for(int i = 0; i < numelems2; i++)
	{
		randelems2[i] = newInt(rand() % 100);
	}

	//Create the random test sets.
	set1 = newSetWithValues(numelems1, randelems1);
	set2 = newSetWithValues(numelems2, randelems2);
	diffset = vdmSetDifference(set1, set2);
	UNWRAP_COLLECTION(diffcol, diffset);

	//Check that the difference is a subset of the original set.
	res = vdmSetSubset(diffset, set1);
	EXPECT_EQ(true, res->value.boolVal);
	vdmFree(res);

	//Check that none of the elements in set2 is in the difference set.
	interset = vdmSetInter(diffset, set2);
	UNWRAP_COLLECTION(intercol, interset);
	EXPECT_EQ(true, intercol->size == 0);

	//Wrap up.
	vdmFree(set1);
	vdmFree(set2);
	vdmFree(diffset);
	for(int i = 0; i < numelems1; i++)
	{
		vdmFree(randelems1[i]);
	}
	for(int i = 0; i < numelems2; i++)
	{
		vdmFree(randelems2[i]);
	}
}
TEST(Expression_SetRandom, setNotInSet)
{
	const int numelems = 10;
	TVP elem = newInt(INT_MAX);
	TVP set = newSetVar(1, elem);
	struct TypedValue *randelems[100], *randelemscpy[100];

	//Make sure that we're checking for the value itself and not the newInt structure.
	vdmFree(elem);
	elem = newInt(INT_MAX);

	TVP res = vdmSetNotMemberOf(set, elem);
	EXPECT_EQ(false, res->value.boolVal);
	vdmFree(elem);
	vdmFree(res);

	elem = newInt(INT_MAX - 1);
	res = vdmSetNotMemberOf(set, elem);
	EXPECT_EQ(true, res->value.boolVal);
	vdmFree(elem);

	for(int i = 0; i < numelems; i++)
	{
		randelems[i] = newInt(rand());
	}

	vdmFree(set);
	set = newSetWithValues(numelems, randelems);

	for(int i = 0; i < numelems; i++)
	{
		randelemscpy[i] = vdmClone(randelems[i]);
	}

	for(int i = 0; i < numelems; i++)
	{
		vdmFree(res);
		res = vdmSetNotMemberOf(set, randelemscpy[i]);
		EXPECT_EQ(false, res->value.boolVal);
	}

	//Wrap up.
	vdmFree(res);
	vdmFree(set);
	for(int i = 0; i < numelems; i++)
	{
		free(randelems[i]);
		free(randelemscpy[i]);
	}
}
Exemple #6
0
Intervals Interval::unionize(Interval& other) {
	Intervals result;
	if (isEmpty()) {
		result.push_back(other);
		return result;
	}
	else if (other.isEmpty()) {
		result.push_back(*this);
		return result;
	}
	else if (intersects(*this, other)) {
		//std::cout << "intersecting" << std::endl;
		Interval newInt(std::min(_min, other._min), std::max(_max, other._max));
		result.push_back(newInt);
		return result;
	}
	else {
		if (_min < other._min) {
			result.push_back(*this);
			result.push_back(other);
			return result;
		}
		else {
			result.push_back(other);
			result.push_back(*this);
			return result;
		}
	}
	return result;
}
TEST(Expression_SetRandom, setProperSubset)
{
	const int numelems1 = 101;
	TVP randelems1[numelems1];
	TVP set1;
	TVP set2;
	TVP res;

	//Generate the random test value collections.
	for(int i = 0; i < numelems1; i++)
	{
		randelems1[i] = newInt(rand());
	}

	//Create test set.
	set1 = newSetWithValues(numelems1, randelems1);

	//Set must not be a proper subset of itself.
	res = vdmSetProperSubset(set1, set1);
	EXPECT_EQ(false, res->value.boolVal);
	vdmFree(res);

	//Get corresponding collection of test set without duplicates.
	UNWRAP_COLLECTION(setnodupscol, set1);
	//Make this into a new but smaller set.
	set2 = newSetWithValues(setnodupscol->size - 1, setnodupscol->value);
	res = vdmSetProperSubset(set2, set1);
	EXPECT_EQ(true, res->value.boolVal);
	vdmFree(res);

	//And a silly non-subset test.
	vdmFree(set1);
	vdmFree(set2);
	set1 = newSetVar(1, newInt(1));
	set2 = newSetVar(1, newInt(2), newInt(3));
	res = vdmSetProperSubset(set1, set2);
	EXPECT_EQ(false, res->value.boolVal);

	//Wrap up.
	for(int i = 0; i < numelems1; i++)
	{
		vdmFree(randelems1[i]);
	}
	vdmFree(set1);
	vdmFree(set2);
	vdmFree(res);
}
Exemple #8
0
LispRef content(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
    WITH_DEBUG(fprintf(stderr, "content\n"));

    char tag;
    read_byte(tag);

    WITH_DEBUG(fprintf(stderr, "  tag: %x\n", tag));

    switch (tag)
    {
        case TC_NULL:
            return nullReference(stream, eos_error_p, eos_value);
        case TC_REFERENCE:
            return prevObject(stream, eos_error_p, eos_value);
        case TC_CLASS:
            return newClass(stream, eos_error_p, eos_value);
        case TC_OBJECT:
            return newObject(stream, eos_error_p, eos_value);
        case TC_STRING:
            return newString(stream, eos_error_p, eos_value);
        case TC_STATE:
            return newState(stream, eos_error_p, eos_value);
        case TC_VECTOR:
            return newVector(stream, eos_error_p, eos_value);
        case TC_STREAM:
            return newStream(stream, eos_error_p, eos_value);
        case TC_RESET:
            return reset(stream, eos_error_p, eos_value);
        case TC_SELF:
            return stream;
        case TC_FUNCTION:
            return newFunction(stream, eos_error_p, eos_value);
        case TC_BYTEVECTOR:
            return newBytevector(stream, eos_error_p, eos_value);
        case TC_INT:
            return newInt(stream, eos_error_p, eos_value);
        case TC_DOUBLE:
            return newDouble(stream, eos_error_p, eos_value);
        case TC_SYMBOL:
            return newSymbol(stream, eos_error_p, eos_value);
        case TC_KEYWORD:
            return newKeyword(stream, eos_error_p, eos_value);
        case TC_CHAR:
            return newChar(stream, eos_error_p, eos_value);
        case TC_CONS:
            return newCons(stream, eos_error_p, eos_value);
        default:
            {
                LispRef str, args;

                eul_allocate_string(str, "unknown tag in ~a");
                eul_allocate_cons(args, stream, eul_nil);
                eul_serial_error(stream, str, args);
                return eul_nil;
            }
    }
}
int main()
{
    int *x = newInt(20);

    std::cout << *x;
    delete x;
    x = NULL;

    return 0;
}
TEST(Expression_Map, mapRngRestrictBy)
{

	TVP arr[2] = {newInt(5), newInt(2)};

	TVP set = newSetWithValues(2, arr);

	//map1: {1|->2,3|->4,6|->7}
	TVP map = createMap1();

	TVP map_res = vdmMapRngRestrictBy(set,map);

	//Get domain
	TVP map_dom = vdmMapDom(map_res);

	//Test if 1 is removed from map domain, because it maps to 2
	TVP res = vdmSetMemberOf(map_dom,newInt(1));

	EXPECT_EQ(false, res->value.boolVal);
}
//Utility functions
//------------------------------------------------
//TODO use correct set construction
static TVP newSet(int size, int* arr)
{
	struct TypedValue** value = (struct TypedValue**) calloc(size, sizeof(struct TypedValue*));

	for (int i = 0; i < size; i++)
	{
		value[i] = newInt(arr[i]);
	}

	return newSetWithValues(size, value);
}
TEST(Expression_Map, mapRng)
{
	//map1: {1|->2,3|->4,6|->7}
	TVP map = createMap1();

	//Get domain
	TVP map_rng = vdmMapRng(map);

	TVP res = vdmSetMemberOf(map_rng,newInt(4));
	EXPECT_EQ(true, res->value.boolVal);
}
TEST(Expression_Map, mapInverse)
{

	//map1: {1|->2,3|->4,6|->7}
	TVP map = createMap1();

	TVP map_inv = vdmMapInverse(map);

	//Get domain
	TVP map_dom = vdmMapDom(map_inv);

	//Test if 1 is removed from map domain
	TVP res = vdmSetMemberOf(map_dom,newInt(1));

	EXPECT_EQ(false, res->value.boolVal);

	//Test if 4 is added to map domain
	res = vdmSetMemberOf(map_dom,newInt(4));

	EXPECT_EQ(true, res->value.boolVal);
}
TVP createMap3()
{
	//{}
	TVP map = newMap();

	//maplet1 7|->8
	TVP key1 = newInt(7);
	TVP val1 =newInt(8);
	vdmMapAdd(map,key1,val1);
	vdmFree(key1);
	vdmFree(val1);

	//maplet2 9|->11
	TVP key2 = newInt(9);
	TVP val2 =newInt(11);
	vdmMapAdd(map,key2,val2);
	vdmFree(key2);
	vdmFree(val2);

	return map;
}
TVP createMap1()
{
	//{}
	TVP map = newMap();

//	//maplet1 1|->2
	TVP key1 = newInt(1);
	TVP val1 =newInt(2);
	vdmMapAdd(map,key1,val1);
	vdmFree(key1);
	vdmFree(val1);

	//maplet2 3|->4
	TVP key2 = newInt(3);
	TVP val2 =newInt(4);
	vdmMapAdd(map,key2,val2);
	vdmFree(key2);
	vdmFree(val2);

	//maplet3 6|->7
	TVP key3 = newInt(6);
	TVP val3 =newInt(7);
	vdmMapAdd(map,key3,val3);
	vdmFree(key3);
	vdmFree(val3);


	return map;
}
TEST(Expression_SetRandom, setEquality)
{
	const int numelems1 = 101;
	TVP randelems1[numelems1];
	TVP set1;
	TVP set2;
	TVP res;

	//Generate the random test value collections.
	for(int i = 0; i < numelems1; i++)
	{
		randelems1[i] = newInt(rand());
	}

	//Create test set.
	set1 = newSetWithValues(numelems1, randelems1);
	set2 = newSetWithValues(numelems1, randelems1);

	//Sets must be equal.
	res = vdmSetEquals(set1, set2);
	EXPECT_EQ(true, res->value.boolVal);
	vdmFree(res);

	//And a silly inequality test.
	vdmFree(set1);
	vdmFree(set2);
	set1 = newSetVar(1, newInt(1));
	set2 = newSetVar(1, newInt(2));
	res = vdmSetEquals(set1, set2);
	EXPECT_EQ(false, res->value.boolVal);

	//Wrap up.
	for(int i = 0; i < numelems1; i++)
	{
		vdmFree(randelems1[i]);
	}
	vdmFree(set1);
	vdmFree(set2);
	vdmFree(res);
}
TEST(Expression_SetRandom, setCard)
{
	const int numelems = 5;
	TVP elems[numelems] = {newInt(1), newInt(2), newInt(2), newInt(3), newInt(INT_MAX)};
	TVP theset;

	//Cardinality of empty set.
	theset = newSetWithValues(0, NULL);
	EXPECT_EQ(0, (vdmSetCard(theset))->value.intVal);
	vdmFree(theset);

	//Cardinality of non-empty set.
	theset = newSetWithValues(numelems, elems);
	EXPECT_EQ(numelems - 1, (vdmSetCard(theset))->value.intVal);

	//Wrap up.
	for(int i = 0; i < numelems; i++)
	{
		vdmFree(elems[i]);
	}
	vdmFree(theset);
}
TEST(Expression_Seq, seqElems)
{
	int arr[] =
	{ 1, 2 };
	TVP t = newSequence(2,arr);

	TVP a = newInt(1);
	TVP b = newInt(2);
	TVP elems = newSetVar(2, a,b);

	TVP res = vdmSeqElems(t);

	TVP tmp = vdmEquals(res,elems);
	EXPECT_EQ(true, tmp->value.boolVal);
	recursiveFree(res);
	vdmFree(a);
	vdmFree(b);
	vdmFree(tmp);
	vdmFree(elems);
//
	recursiveFree(t);
}
Exemple #19
0
TVP vdmRem(TVP a,TVP b)
{	ASSERT_CHECK_NUMERIC(a);
ASSERT_CHECK_NUMERIC(b);

//See https://github.com/overturetool/overture/blob/development/core/interpreter/src/main/java/org/overture/interpreter/eval/BinaryExpressionEvaluator.java#L628
ASSERT_CHECK_INT(a);
ASSERT_CHECK_INT(b);

int av = toDouble(a);
int bv = toDouble(b);

return newInt(av-bv*divi(av,bv));
}
TVP newSequence(int size, int* arr)
{
	TVP seq = newSeq(size);

	struct Collection* col =(struct Collection*) seq->value.ptr;

	for (int i = 0; i < size; i++)
	{
		col->value[i] = newInt(arr[i]);
	}

	return seq;
}
TEST(Expression_Map, mapDomRestrictTo)
{

	TVP arr[2] = {newInt(1), newInt(6)};

	TVP set = newSetWithValues(2, arr);

	//map1: {1|->2,3|->4,6|->7}
	TVP map = createMap1();

	TVP map_res = vdmMapDomRestrictTo(set,map);

	// Get domain
	TVP map_dom = vdmMapDom(map_res);
	TVP res = vdmSetMemberOf(map_dom,newInt(3));

	EXPECT_EQ(false, res->value.boolVal);

	vdmFree(res);
	res = vdmSetMemberOf(map_dom, newInt(1));
	EXPECT_EQ(true, res->value.boolVal);
	vdmFree(res);
}
TEST(Expression_Map, mapDom)
{
	//map1: {1|->2,3|->4,6|->7}
	TVP map = createMap1();

	//Get domain (returns a set)
	TVP map_dom = vdmMapDom(map);

	TVP res = vdmSetMemberOf(map_dom,newInt(3));
	EXPECT_EQ(true, res->value.boolVal);

	recursiveFree(map_dom);
	recursiveFree(res);
}
TVP productExp()
{
	TVP tmp1 = newProduct(2);

	TVP tmp1_1 = newInt(1);
	productSet(tmp1,1,tmp1_1);
	recursiveFree(tmp1_1);

	TVP tmp1_2 = newInt(2);
	productSet(tmp1,2,tmp1_2);
	recursiveFree(tmp1_2);


	TVP tmp2_1 = newInt(1);
	TVP tmp2_2 = newInt(2);
	TVP args[2] = {tmp2_1,tmp2_2};
	TVP tmp2 = newProductWithValues(2,args);
	recursiveFree(tmp2_1);
	recursiveFree(tmp2_2);

//
//	TVP tmp2_1 = newInt(1);
//	productSet(tmp2,1,tmp2_1);
//	recursiveFree(tmp2_1);
//
//	TVP tmp2_2 = newInt(2);
//	productSet(tmp2,2,tmp2_2);
//	recursiveFree(tmp2_2);

	TVP res =newBool(equals(tmp1,tmp2));

	//scope cleanup
	recursiveFree(tmp1);
	recursiveFree(tmp2);

	return res;
}
TEST(Expression_Map, mapOverride)
{
	//map1: {1|->2,3|->4,6|->7}
	TVP map1 = createMap1();

	//map2: {6|->8, 9|->11}
	TVP map2 = createMap2();

	//Get domain
	TVP map = vdmMapOverride(map1,map2);

	TVP res = vdmMapApply(map,newInt(6));

	EXPECT_EQ(8, res->value.intVal);
}
TEST(Expression_Seq, seqIndex)
{
	int arr[] =
	{ 1, 2 };
	TVP t = newSequence(2,arr);

	TVP index = newInt(2);
	TVP res = vdmSeqIndex(t,index);

	EXPECT_EQ(2, res->value.intVal);
	recursiveFree(res);
//
	recursiveFree(t);
	recursiveFree(index);
}
Exemple #26
0
TVP vdmSeqInds(TVP seq)
{
	ASSERT_CHECK(seq);
	UNWRAP_COLLECTION(col,seq);

	struct TypedValue** value = (struct TypedValue**) calloc(col->size, sizeof(struct TypedValue*));

	//copy  list
	for (int i = 0; i < col->size; i++)
	{
		value[i] = newInt(i+1);
	}

	TVP indsVal = newSetWithValues(col->size, value);

	return indsVal;
}
Exemple #27
0
TVP vdmAbs(TVP arg)
{
	ASSERT_CHECK_NUMERIC(arg);

	switch(arg->type)
	{
	case VDM_INT:
	case VDM_NAT:
	case VDM_NAT1:
		return newInt(abs(arg->value.intVal));
	case VDM_REAL:
		return newReal(fabs(arg->value.doubleVal));
	default:
		FATAL_ERROR("Invalid type");
		return NULL;
	}
}
int main(int argc, char **argv)
{

	Solution sol;
	// [1,5],[6,8]], [5,6]
	
	vector<Interval> intervals;
	Interval newInt(1, 5);
	Interval newInt2(6, 8);
	intervals.push_back(newInt);
	intervals.push_back(newInt2);

	Interval newInterval(5, 6);

	sol.insert(intervals, newInterval);

	return 0;
}
Exemple #29
0
	langObject ArrayBuffer_Bracket(NativeFunction* func, std::vector<langObject> &arg)
	{
		if (arg.size() != 2)
		{
			throw langRuntimeException("引数の数が違う");
		}
		ArrayBufferClassObject* _this = (ArrayBufferClassObject*)func->thisscope->_this;
		unsigned char *ptr = (unsigned char*)ArrayBufferGetPointer(_this);
		int i1 = Int::toInt(arg[0]);
		unsigned char *obj = &ptr[i1];
		langObject i2 = arg[1];
		if (i2->type->TypeEnum == _Type)
		{
			ObjectType* type = (ObjectType*)i2;
			switch (type->TypeClass.TypeEnum)
			{
				case lang::_Object:
					break;
				case lang::_Int:
					return newInt(*(int*)obj);
				case lang::_String:
					return newString((char*)*((size_t*)obj));
				case lang::_Char:
					return new Char(*(char*)obj);
				case lang::_WChar:
					return new WChar(*(wchar_t*)obj);
				case lang::_Double:
					return newDouble(*(double*)obj);
				case lang::_Array:
					break;
				case lang::_Class:
					break;
				case lang::_ClassObject:
					break;
				default:
					throw langRuntimeException("get arraybuffer 不可");
					break;
			}
		}
		return NULLOBJECT;
	}
TVP mapApply()
{
	TVP res = NULL;
	//{1|->2,3|->4}(1)=2

	//{}
	TVP map = newMap();

	//maplet1 1|->2
	TVP key1 = newInt(1);
	TVP val1 =newInt(2);
	vdmMapAdd(map,key1,val1);
	vdmFree(key1);
	vdmFree(val1);

	//maplet2 3|->4
	TVP key2 = newInt(3);
	TVP val2 =newInt(4);
	vdmMapAdd(map,key2,val2);
	vdmFree(key2);
	vdmFree(val2);

	//apply id
	TVP tmp1 = newInt(1);

	TVP tmp2 = vdmMapApply(map,tmp1);
	vdmFree(tmp1);

	TVP tmp3 = newInt(2);

	TVP tmp4 = newBool(equals(tmp2,tmp3));


	res = vdmClone(tmp4);
	vdmFree(tmp4);
	vdmFree(tmp3);
	vdmFree(tmp2);

	return res;
}