Skip to content

Commit

Permalink
Implementation of newMapVarToGrow() to support map comprehensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Bandur committed Oct 25, 2016
1 parent 9b33765 commit 2df0af9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
33 changes: 31 additions & 2 deletions c/vdmclib/src/main/VdmMap.c
Expand Up @@ -30,6 +30,7 @@
#include "VdmMap.h"

#include <stdio.h> //FIXME remove all printf!
#include <stdarg.h>

#define ASSERT_CHECK(s) assert(s->type == VDM_MAP && "Value is not a map")

Expand Down Expand Up @@ -213,7 +214,6 @@ TVP vdmMapRng(TVP map)
#else



hashtable_t *ht_create( int size ) {

hashtable_t *hashtable = NULL;
Expand Down Expand Up @@ -387,7 +387,7 @@ TVP ht_get( hashtable_t *hashtable, TVP key ) {



struct TypedValue* newMap()
TVP newMap()
{
struct Map* ptr = (struct Map*) malloc(sizeof(struct Map));
//TODO: work out initial size.
Expand All @@ -399,6 +399,35 @@ struct TypedValue* newMap()
}


TVP newMapVarToGrow(size_t size, size_t expected_size, ...)
{
struct Map* ptr = (struct Map*) malloc(sizeof(struct Map));
TVP key;
TVP value;
TVP theMap;

ptr->table = ht_create(expected_size);
theMap = newTypeValue(VDM_MAP, (TypedValueType){ .ptr = ptr });

va_list argList;
va_start(argList, size * 2);

for(int i = 0; i < size; i++)
{
key = vdmClone(va_arg(argList, TVP));
value = vdmClone(va_arg(argList, TVP));

vdmMapAdd(theMap, key, value);

vdmFree(key);
vdmFree(value);
}
va_end(argList);

return theMap;

}


void vdmMapAdd(TVP map, TVP key, TVP value)
{
Expand Down
2 changes: 2 additions & 0 deletions c/vdmclib/src/main/VdmMap.h
Expand Up @@ -77,6 +77,8 @@ struct TypedValue* newMap();
//util method for adding maplets
void vdmMapAdd(TVP map,TVP key, TVP value);

TVP newMapVarToGrow(size_t, size_t, ...);

//VDM map operators
TVP vdmMapDom(TVP map);
TVP vdmMapRng(TVP map);
Expand Down
36 changes: 35 additions & 1 deletion c/vdmclib/src/test/ExpressionsMap_Tests.cpp
Expand Up @@ -34,7 +34,7 @@ TVP createMap1()
//{}
TVP map = newMap();

// //maplet1 1|->2
// //maplet1 1|->2
TVP key1 = newInt(1);
TVP val1 =newInt(2);
vdmMapAdd(map,key1,val1);
Expand Down Expand Up @@ -383,3 +383,37 @@ TEST(Expression_Map, mapInEquals)
bool map_not_eq2 = vdmMapInEquals(map1,map3);
EXPECT_EQ(true, map_not_eq2);
}


TEST(Expression_Map, newMapVarToGrow)
{
TVP theMap = newMapVarToGrow(3, 10, newChar('a'), newInt(1), newChar('b'), newInt(2), newChar('c'), newInt(3));
TVP val;
TVP res;

val = vdmMapApply(theMap, newChar('a'));
res = vdmEquals(val, newInt(1));
EXPECT_EQ(true, res->value.boolVal);
vdmFree(val);
vdmFree(res);

val = vdmMapApply(theMap, newChar('b'));
res = vdmEquals(val, newInt(2));
EXPECT_EQ(true, res->value.boolVal);
vdmFree(val);
vdmFree(res);

val = vdmMapApply(theMap, newChar('c'));
res = vdmEquals(val, newInt(3));
EXPECT_EQ(true, res->value.boolVal);
vdmFree(val);
vdmFree(res);

val = vdmMapApply(theMap, newChar('c'));
res = vdmEquals(val, newInt(4));
EXPECT_EQ(false, res->value.boolVal);
vdmFree(val);
vdmFree(res);

vdmFree(theMap);
}

0 comments on commit 2df0af9

Please sign in to comment.