(MOD) Changed returning type of Var_Delete and Array_Delete from "void" to "Status": It requires more plaination when encountering non-alive instances.
(MOD) Implemented Array_Equals. (MOD) Replaced struct member "description" from InstanceStillAlive from "Given instance was yet alive." to "Given instance was still alive.". (MOD) Removed String_GetIdx and String_SetIdx: Meaningless functions. (MOD) Changed the assignment value of struct member "identity" of macro unknown, normal and error from "nameof(e)" to "e.identity".
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -22,3 +22,4 @@ CompoundTest
|
|||||||
ccwarn
|
ccwarn
|
||||||
genwarn.sh
|
genwarn.sh
|
||||||
test.sh
|
test.sh
|
||||||
|
expansion_formatted.txt
|
||||||
|
@@ -14,9 +14,7 @@ DEFSTATUS(InvalidArrayLength, 1, "Given length is invalid.", STATUS_ERROR, &Erro
|
|||||||
|
|
||||||
Status Array_Create(Array *inst, int len, size_t size);
|
Status Array_Create(Array *inst, int len, size_t size);
|
||||||
Status Array_CopyOf(Array *inst, Array *other);
|
Status Array_CopyOf(Array *inst, Array *other);
|
||||||
void Array_Delete(Array *inst);
|
Status Array_Delete(Array *inst);
|
||||||
// Status Array_GetIdx(Array *inst, Var *store, int index);
|
|
||||||
// Status Array_SetIdx(Array *inst, Var *source, int index);
|
|
||||||
bool Array_Equals(Array *arr1, Array *arr2);
|
bool Array_Equals(Array *arr1, Array *arr2);
|
||||||
|
|
||||||
Status ArrayUtils_Insert(Array *inst, Var *item, int index);
|
Status ArrayUtils_Insert(Array *inst, Var *item, int index);
|
||||||
|
@@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
Status Array_Create(Array *inst, int len, size_t size)
|
Status Array_Create(Array *inst, int len, size_t size)
|
||||||
{
|
{
|
||||||
|
nonull(inst, apply(UnavailableInstance));
|
||||||
|
|
||||||
/* Skip the living instances. */
|
/* Skip the living instances. */
|
||||||
state(inst && inst->alive, apply(InstanceStillAlive));
|
state(inst && inst->alive, apply(InstanceStillAlive));
|
||||||
|
|
||||||
@@ -101,54 +103,45 @@ Status Array_CopyOf(Array *inst, Array *other)
|
|||||||
return apply(NormalStatus);
|
return apply(NormalStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Array_Delete(Array *inst)
|
Status Array_Delete(Array *inst)
|
||||||
{
|
{
|
||||||
svoid(!inst || !inst->alive);
|
nonull(!inst, apply(UnavailableInstance));
|
||||||
|
state(!inst->alive, apply(InstanceNotAlive));
|
||||||
|
|
||||||
|
/* Iterate through each member and delete them. */
|
||||||
for (register int i = 0; i < inst->len; i++) {
|
for (register int i = 0; i < inst->len; i++) {
|
||||||
Var_Delete(&inst->members[i]);
|
fail(Var_Delete(&inst->members[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
free(inst->members);
|
free(inst->members);
|
||||||
|
inst->members = NULL;
|
||||||
inst->alive = false;
|
inst->alive = false;
|
||||||
inst->len = 0;
|
inst->len = 0;
|
||||||
|
|
||||||
|
return apply(NormalStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Status Array_GetIdx(Array *inst, Var *store, int index)
|
bool Array_Equals(Array *arr1, Array *arr2)
|
||||||
// {
|
{
|
||||||
// nonull(inst, apply(UnavailableInstance));
|
/* Skip unavailable instance and parameter. */
|
||||||
// nonull(store, apply(annot(UnavailableParameter,
|
state(!arr1 || !arr2, false);
|
||||||
// "Given store was unavailable.")));
|
|
||||||
// state(!store->alive, apply(annot(InstanceNotAlive,
|
|
||||||
// "Cannot store into a non-alive var.")));
|
|
||||||
// state(index >= inst->len || index < 0, apply(ArrayIndexOutOfBound));
|
|
||||||
|
|
||||||
// // *store = inst->members[index];
|
/* Skip when arr1 and arr2 have different length. */
|
||||||
// store->addr = inst->members[index].addr;
|
state(arr1->len != arr2->len, false);
|
||||||
// store->size = inst->members[index].size;
|
|
||||||
|
|
||||||
// return apply(NormalStatus);
|
/* Skip when operation is not supported. */
|
||||||
// }
|
state(!arr1->alive || !arr2->alive, false);
|
||||||
|
|
||||||
// Status Array_SetIdx(Array *inst, Var *source, int index)
|
|
||||||
// {
|
|
||||||
// nonull(inst, apply(UnavailableInstance));
|
|
||||||
// nonull(source, apply(annot(UnavailableParameter,
|
|
||||||
// "Given store was unavailable.")));
|
|
||||||
// state(!source->alive, apply(annot(InstanceNotAlive,
|
|
||||||
// "Cannot assign with a non-alive var.")));
|
|
||||||
// state(index >= inst->len || index < 0, apply(ArrayIndexOutOfBound));
|
|
||||||
|
|
||||||
// // inst->members[index] = *source;
|
/* Iterate through each member for comparison. */
|
||||||
// inst->members[index].addr = source->addr;
|
for (register int i = 0; i < arr1->len; i++) {
|
||||||
// inst->members[index].size = source->size;
|
if (!Var_Equals(&arr1->members[i], &arr2->members[i])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// return apply(NormalStatus);
|
/* Compare rest of the struct member. */
|
||||||
// }
|
return (arr1->alive == arr2->alive);
|
||||||
|
}
|
||||||
bool Array_Equals(Array *arr1, Array *arr2);
|
|
||||||
|
|
||||||
|
|
||||||
Status ArrayUtils_Insert(Array *inst, Var *item, int index);
|
Status ArrayUtils_Insert(Array *inst, Var *item, int index);
|
||||||
|
|
||||||
@@ -171,6 +164,3 @@ Status ArrayUtils_Split(Array *inst, Array *fore, Array *rear, int index);
|
|||||||
Status ArrayUtils_Revert(Array *inst);
|
Status ArrayUtils_Revert(Array *inst);
|
||||||
|
|
||||||
bool ArrayUtils_IsEmpty(Array *inst);
|
bool ArrayUtils_IsEmpty(Array *inst);
|
||||||
|
|
||||||
bool ArrayUtils_IsBlank(Array *inst);
|
|
||||||
|
|
||||||
|
@@ -190,7 +190,7 @@ DEFSTATUS(RuntimeError, 1,
|
|||||||
STATUS_ERROR, &ErrorStatus);
|
STATUS_ERROR, &ErrorStatus);
|
||||||
|
|
||||||
DEFSTATUS(InstanceStillAlive, 1,
|
DEFSTATUS(InstanceStillAlive, 1,
|
||||||
"Given instance was yet alive.",
|
"Given instance was still alive.",
|
||||||
STATUS_ERROR, &RuntimeError);
|
STATUS_ERROR, &RuntimeError);
|
||||||
|
|
||||||
DEFSTATUS(InstanceNotAlive, 1,
|
DEFSTATUS(InstanceNotAlive, 1,
|
||||||
|
@@ -19,15 +19,13 @@ typedef struct {
|
|||||||
StringEncoding encoding;
|
StringEncoding encoding;
|
||||||
} String;
|
} String;
|
||||||
|
|
||||||
/* Elementary. */
|
|
||||||
Status String_Create(String *inst, int len);
|
Status String_Create(String *inst, int len);
|
||||||
Status String_CopyOf(String *inst, String *other);
|
Status String_CopyOf(String *inst, String *other);
|
||||||
Status String_Delete(String *inst);
|
Status String_Delete(String *inst);
|
||||||
Status String_GetIdx(String *inst, Char *item, int index);
|
Status String_Encode(String *inst, StringEncoding encoding);
|
||||||
Status String_SetIdx(String *inst, Char *item, int index);
|
Status String_Decode(String *inst, StringEncoding encoding);
|
||||||
Status String_Literalise(String *inst, String *store);
|
Status String_Literalise(String *inst, String *store);
|
||||||
|
|
||||||
/* Extensional. */
|
|
||||||
Status StringUtils_FromInteger(String *inst, int value, int base);
|
Status StringUtils_FromInteger(String *inst, int value, int base);
|
||||||
Status StringUtils_FromShortInteger(String *inst, short int value, int base);
|
Status StringUtils_FromShortInteger(String *inst, short int value, int base);
|
||||||
Status StringUtils_FromLongInteger(String *inst, long int value, int base);
|
Status StringUtils_FromLongInteger(String *inst, long int value, int base);
|
||||||
@@ -77,10 +75,8 @@ Status StringUtils_ToUnsignedComplexLongLongInteger(String *inst, unsigned _Comp
|
|||||||
Status StringUtils_ToAddress(String *inst, void **store);
|
Status StringUtils_ToAddress(String *inst, void **store);
|
||||||
Status StringUtils_ToCharBuff(String *inst, char const *buff);
|
Status StringUtils_ToCharBuff(String *inst, char const *buff);
|
||||||
Status StringUtils_ToWideCharBuff(String *inst, wchar_t const *wbuff);
|
Status StringUtils_ToWideCharBuff(String *inst, wchar_t const *wbuff);
|
||||||
// Status StringUtils_Format(String *inst, const String *restrict fmt, ...);
|
|
||||||
Status StringUtils_Tokenise(String *inst, const String *delim, String *store);
|
Status StringUtils_Tokenise(String *inst, const String *delim, String *store);
|
||||||
Status String_Encode(String *inst, StringEncoding encoding) throws(UnsupportedEncoding EncodingError DecodingError);
|
bool StringUtils_IsBlank(String *inst);
|
||||||
Status String_Decode(String *inst, StringEncoding encoding) throws(UnsupportedEncoding EncodingError DecodingError);
|
|
||||||
int StringUtils_Compare(String *a, String *b);
|
int StringUtils_Compare(String *a, String *b);
|
||||||
|
|
||||||
static Status StringConversionPrecisionError = {
|
static Status StringConversionPrecisionError = {
|
||||||
@@ -93,13 +89,4 @@ static Status StringConversionPrecisionError = {
|
|||||||
.prev = (Status *)&ImprecisionError
|
.prev = (Status *)&ImprecisionError
|
||||||
};
|
};
|
||||||
|
|
||||||
// # define string(str) ((String) {\
|
|
||||||
// .data =
|
|
||||||
// })
|
|
||||||
|
|
||||||
typedef Array(int) InfiniteInteger;
|
|
||||||
typedef Array(double) InfiniteFloatingPoint;
|
|
||||||
typedef Array(_Complex double) InfintieComplex;
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* COMPOUND_STRING_H */
|
#endif /* COMPOUND_STRING_H */
|
||||||
|
@@ -52,9 +52,9 @@ typedef struct {
|
|||||||
|
|
||||||
Status Var_Create(Var *inst, size_t size) throws(InsufficientMemory);
|
Status Var_Create(Var *inst, size_t size) throws(InsufficientMemory);
|
||||||
Status Var_CopyOf(Var *inst, Var *other);
|
Status Var_CopyOf(Var *inst, Var *other);
|
||||||
|
Status Var_Delete(Var *inst);
|
||||||
Status Var_Literalise(Var *inst, char *buff);
|
Status Var_Literalise(Var *inst, char *buff);
|
||||||
bool Var_Equals(Var *a, Var *b);
|
bool Var_Equals(Var *a, Var *b);
|
||||||
void Var_Delete(Var *inst);
|
|
||||||
|
|
||||||
void VarUtils_Swap(Var *v1, Var *v2);
|
void VarUtils_Swap(Var *v1, Var *v2);
|
||||||
// bool VarUtils_IsIdentityLegal(char *identity);
|
// bool VarUtils_IsIdentityLegal(char *identity);
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
#include <Compound/status.h>
|
||||||
#include <Compound/var.h>
|
#include <Compound/var.h>
|
||||||
|
|
||||||
Status Var_Create(Var *inst, size_t size)
|
Status Var_Create(Var *inst, size_t size)
|
||||||
@@ -14,22 +15,6 @@ Status Var_Create(Var *inst, size_t size)
|
|||||||
return apply(NormalStatus);
|
return apply(NormalStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Status Var_Create(Var *inst, void *addr, size_t size, char *identity)
|
|
||||||
// {
|
|
||||||
// /* Skip when inst is unavailable. */
|
|
||||||
// nonull(inst, apply(UnavailableInstance));
|
|
||||||
// /* Skip when identity is unavailable. */
|
|
||||||
// nonull(identity, NullPointerAccounted);
|
|
||||||
// /* Skip when identity does not pass the examine. */
|
|
||||||
// state(!VarUtils_IsIdentityLegal(identity), IllegalVarIdentity);
|
|
||||||
|
|
||||||
// inst->addr = addr;
|
|
||||||
// inst->size = size;
|
|
||||||
// *inst->identity = *identity;
|
|
||||||
|
|
||||||
// return apply(NormalStatus);
|
|
||||||
// }
|
|
||||||
|
|
||||||
Status Var_CopyOf(Var *inst, Var *other)
|
Status Var_CopyOf(Var *inst, Var *other)
|
||||||
{
|
{
|
||||||
/* Skip when inst or other is unavailable. */
|
/* Skip when inst or other is unavailable. */
|
||||||
@@ -45,34 +30,17 @@ Status Var_CopyOf(Var *inst, Var *other)
|
|||||||
return apply(NormalStatus);
|
return apply(NormalStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Var_Delete(Var *inst)
|
Status Var_Delete(Var *inst)
|
||||||
{
|
{
|
||||||
svoid(!inst || !inst->alive);
|
nonull(!inst, apply(UnavailableInstance));
|
||||||
|
state(!inst->alive, apply(InstanceNotAlive));
|
||||||
|
|
||||||
free(inst->addr);
|
free(inst->addr);
|
||||||
inst->alive = false;
|
inst->alive = false;
|
||||||
inst->addr = NULL;
|
inst->addr = NULL;
|
||||||
inst->size = 0;
|
inst->size = 0;
|
||||||
}
|
|
||||||
|
|
||||||
// void Var_Delete(Var *inst)
|
|
||||||
// {
|
|
||||||
// /* Skip when inst or inst->addr is unavailable. */
|
|
||||||
// svoid(!inst || inst->addr == NULL);
|
|
||||||
|
|
||||||
// inst->addr = NULL;
|
|
||||||
// inst->size = 0;
|
|
||||||
// *inst->identity = 0;
|
|
||||||
// }
|
|
||||||
|
|
||||||
void VarUtils_Swap(Var *v1, Var *v2)
|
|
||||||
{
|
|
||||||
/* Skip when v1 or v2 is unavailable. */
|
|
||||||
svoid(!v1 || !v2);
|
|
||||||
|
|
||||||
Var v3 = *v1;
|
return apply(NormalStatus);
|
||||||
*v1 = *v2;
|
|
||||||
*v2 = v3;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Status Var_Literalise(Var *inst, char *buff)
|
Status Var_Literalise(Var *inst, char *buff)
|
||||||
@@ -96,32 +64,12 @@ bool Var_Equals(Var *a, Var *b)
|
|||||||
return (a->addr == b->addr && a->size == b->size);
|
return (a->addr == b->addr && a->size == b->size);
|
||||||
}
|
}
|
||||||
|
|
||||||
// bool VarUtils_IsIdentityLegal(char *identity)
|
void VarUtils_Swap(Var *v1, Var *v2)
|
||||||
// {
|
{
|
||||||
// /* Skip when identity is unavailable. */
|
/* Skip when v1 or v2 is unavailable. */
|
||||||
// state(identity == NULL, false);
|
svoid(!v1 || !v2);
|
||||||
|
|
||||||
// const int len = strlen(identity);
|
Var v3 = *v1;
|
||||||
|
*v1 = *v2;
|
||||||
// /* Skip when identity is empty. */
|
*v2 = v3;
|
||||||
// state(len == 0, false);
|
}
|
||||||
|
|
||||||
// /* Skip when the first char is not within alphabet. */
|
|
||||||
// state(ATRANGE('a', 'z', identity[0])
|
|
||||||
// || ATRANGE('A', 'Z', identity[0]), false);
|
|
||||||
|
|
||||||
// /* Skip when the length of identity is greater that VAR_IDENTITY_LENGTH. */
|
|
||||||
// state(len > VAR_IDENTITY_LENGTH, false);
|
|
||||||
|
|
||||||
// /* Skip when identity has space and illegal characters in it. */
|
|
||||||
// const int illegal_len = strlen(VAR_IDENTITY_ILLEGAL_CHAR);
|
|
||||||
// for (register int i = 0; i < len; i++) {
|
|
||||||
// for (register int j = 0; j < illegal_len; j++) {
|
|
||||||
// if (identity[i] == VAR_IDENTITY_ILLEGAL_CHAR[j]) {
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return true;
|
|
||||||
// }
|
|
||||||
|
8
common.h
8
common.h
@@ -85,7 +85,7 @@
|
|||||||
|
|
||||||
/* Create a new UnknownStatus on the fly. */
|
/* Create a new UnknownStatus on the fly. */
|
||||||
# define unknown(e, c, v) ((Status) {\
|
# define unknown(e, c, v) ((Status) {\
|
||||||
.identity = nameof(e),\
|
.identity = e.identity,\
|
||||||
.value = v,\
|
.value = v,\
|
||||||
.description = c,\
|
.description = c,\
|
||||||
.characteristic = STATUS_UNKNOWN,\
|
.characteristic = STATUS_UNKNOWN,\
|
||||||
@@ -95,7 +95,7 @@
|
|||||||
|
|
||||||
/* Create a new NormalStatus on the fly. */
|
/* Create a new NormalStatus on the fly. */
|
||||||
# define normal(e, c) ((Status) {\
|
# define normal(e, c) ((Status) {\
|
||||||
.identity = nameof(e),\
|
.identity = e.identity,\
|
||||||
.value = 0,\
|
.value = 0,\
|
||||||
.description = c,\
|
.description = c,\
|
||||||
.characteristic = STATUS_NORMAL,\
|
.characteristic = STATUS_NORMAL,\
|
||||||
@@ -105,7 +105,7 @@
|
|||||||
|
|
||||||
/* Create a new ErrorStatus on the fly. */
|
/* Create a new ErrorStatus on the fly. */
|
||||||
# define error(e, c) ((Status) {\
|
# define error(e, c) ((Status) {\
|
||||||
.identity = nameof(e),\
|
.identity = e.identity,\
|
||||||
.value = e.value,\
|
.value = e.value,\
|
||||||
.description = c,\
|
.description = c,\
|
||||||
.characteristic = STATUS_ERROR,\
|
.characteristic = STATUS_ERROR,\
|
||||||
@@ -113,7 +113,7 @@
|
|||||||
.prev = e.prev\
|
.prev = e.prev\
|
||||||
})
|
})
|
||||||
|
|
||||||
/* Extend the Status chain by giving 'p' for "predecessor" and 'e' for "Eval-Status". */
|
/* Replace the prev of e with p. */
|
||||||
# define extend(p, e) ((Status) {\
|
# define extend(p, e) ((Status) {\
|
||||||
.identity = e.identity,\
|
.identity = e.identity,\
|
||||||
.value = p.value,\
|
.value = p.value,\
|
||||||
|
35
test.c
35
test.c
@@ -21,7 +21,34 @@ void __DESTRUCT__() {}
|
|||||||
|
|
||||||
Status Main(void)
|
Status Main(void)
|
||||||
{
|
{
|
||||||
Memory mem;
|
|
||||||
|
|
||||||
|
return apply(NormalStatus);
|
||||||
|
}
|
||||||
|
|
||||||
|
Status MainArrayComparisonTest(void)
|
||||||
|
{
|
||||||
|
Array arr1 = EMPTY;
|
||||||
|
fail(Array_Create(&arr1, 10, 10));
|
||||||
|
|
||||||
|
Array arr2 = EMPTY;
|
||||||
|
fail(Array_Create(&arr2, 10, 10));
|
||||||
|
|
||||||
|
if (Array_Equals(&arr1, &arr2)) {
|
||||||
|
cat("Equal!");
|
||||||
|
} else {
|
||||||
|
cat("Not equal!");
|
||||||
|
}
|
||||||
|
|
||||||
|
Array_Delete(&arr2);
|
||||||
|
Array_Delete(&arr1);
|
||||||
|
|
||||||
|
return apply(NormalStatus);
|
||||||
|
}
|
||||||
|
|
||||||
|
Status MainMemoryOperationTest(void)
|
||||||
|
{
|
||||||
|
Memory mem = EMPTY;
|
||||||
fail(Memory_Create(&mem, sizeof(double)));
|
fail(Memory_Create(&mem, sizeof(double)));
|
||||||
|
|
||||||
fail(Memory_Allocate(&mem));
|
fail(Memory_Allocate(&mem));
|
||||||
@@ -56,11 +83,11 @@ Status MainMacroFailsTest(void)
|
|||||||
|
|
||||||
Status MainArrayCreateAndDeleteWithModulatedMemberAccessing(void)
|
Status MainArrayCreateAndDeleteWithModulatedMemberAccessing(void)
|
||||||
{
|
{
|
||||||
Array arr;
|
Array arr = EMPTY;
|
||||||
fail(Array_Create(&arr, 8, sizeof(int)));
|
fail(Array_Create(&arr, 8, sizeof(int)));
|
||||||
|
|
||||||
for (register int i = 0; i < arr.len; i++) {
|
for (register int i = 0; i < arr.len; i++) {
|
||||||
Var current;
|
Var current = EMPTY;
|
||||||
|
|
||||||
// fails(Var_Create(¤t, arr.members[0].size),
|
// fails(Var_Create(¤t, arr.members[0].size),
|
||||||
// "Failed to create Var current.");
|
// "Failed to create Var current.");
|
||||||
@@ -130,7 +157,7 @@ Status MainArrayCreateAndDeleteWithTraditionalMemberAccessing(void)
|
|||||||
// // Array_Delete(&arr);
|
// // Array_Delete(&arr);
|
||||||
|
|
||||||
|
|
||||||
Array arr;
|
Array arr = EMPTY;
|
||||||
fail(Array_Create(&arr, 8, sizeof(long long)));
|
fail(Array_Create(&arr, 8, sizeof(long long)));
|
||||||
|
|
||||||
for (register int i = 0; i < arr.len; i++) {
|
for (register int i = 0; i < arr.len; i++) {
|
||||||
|
Reference in New Issue
Block a user