-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.cpp
More file actions
98 lines (81 loc) · 2.28 KB
/
Copy patherror.cpp
File metadata and controls
98 lines (81 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "stack.h"
#include "error.h"
void StackDump(struct Stack* stk, const char *file, int line, const char *function)
{
StackAssert(stk);
printf("\nTime is %s\n", __TIME__);
printf("StackDump called from %s (%d) %s\n", function, line, file);
printf("Stack[%p] \"stk\" in function - %s.\n\n", (void*)stk, function);
printf("--------------------------------------------------------------------------\n");
printf("Struct:\n");
printf("\tsize = %d\n", stk->size);
printf("\tcapacity = %lu\n", stk->capacity);
printf("\tAddress of data[] = %p\n", (void*)stk->data);
for (size_t i = 0; i < stk->capacity; i++) {
printf("\tdata[%zu] = %lf\n", i, stk->data[i]);
}
printf("--------------------------------------------------------------------------\n");
}
void StackAssert(Stack* stk)
{
if (stk->size < 0)
{
stk->err = Size;
printf("Error: %s\n", StackError(stk));
assert(0);
}
if (stk->capacity < 0)
{
stk->err = Size;
printf("Error: %s\n", StackError(stk));
assert(0);
}
if (stk->data == nullptr)
{
stk->err = NullPointer;
printf("Error: %s\n", StackError(stk));
assert(0);
}
#ifdef HASH
uint64_t new_hash = StackHashFNV(stk);
if (stk->hash != new_hash)
{
stk->err = HashError;
}
#endif
if (stk->err != NoError)
{
printf("Error: %s\n", StackError(stk));
StackDumpMacro(stk);
assert(0);
}
}
uint64_t StackHashFNV(Stack* stk)
{
uint64_t hash = 0x811C9DC5;
uint64_t HASH_CONST = 0xEDA;
for (size_t i = 0; i < stk->size; i++)
{
hash ^= (uint32_t)stk->data[i];
hash *= HASH_CONST;
}
stk->hash = hash;
return stk->hash;
}
const char* StackError(Stack* stk)
{
switch (stk->err)
{
case Overflow: return "Stack Overflow";
case Size: return "Size/Capacity is not correct";
case Underflow: return "Stack Underflow";
case NoError: return "Stack is working fine";
case NullPointer: return "Null Pointer Error";
case CanaryError: return "Canary was poisoned";
case HashError: return "Hash is not equal";
default: return "Unknown Error";
}
}