• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

thoni56 / c-xrefactory / 1797

20 May 2026 01:10PM UTC coverage: 85.479% (+0.2%) from 85.242%
1797

push

travis-ci

thoni56
[memory][overflow] Rename to outOfMemoryErrorHandler

16317 of 19089 relevant lines covered (85.48%)

15996254.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.88
src/memory_tests.c
1
#include <cgreen/assertions.h>
2
#include <cgreen/cgreen.h>
3
#include <cgreen/constraint_syntax_helpers.h>
4

5
#include <setjmp.h>
6

7
#include "commons.h"
8
#include "memory.h"
9

10
#include "commons.mock" /* For fatalError() */
11
#include "globals.mock"
12
#include "log.mock"
13

14

15
static bool fatalErrorAllowed = false;
16
static bool fatalErrorCalled  = false;
17
static void myFatalError(int errCode, char *mess, int exitStatus, char *file, int line) {
1✔
18
    if (!fatalErrorAllowed)
1✔
19
        fail_test("FatalError() called");
×
20
    fatalErrorCalled = true;
1✔
21
}
1✔
22

23

24
static jmp_buf internalCheckJmpbuf;
25
static bool internalCheckFailAllowed = false;
26
static bool internalCheckFailCalled  = false;
27
static void myInternalCheckFailed(char *expr, char *file, int line) {
2✔
28
    if (!internalCheckFailAllowed)
2✔
29
        internalCheckFail(expr, file, line);
×
30
    internalCheckFailCalled = true;
2✔
31
    longjmp(internalCheckJmpbuf, 1);
2✔
32
}
33

34

35
Describe(Memory);
6✔
36
BeforeEach(Memory) {
6✔
37
    log_set_level(LOG_ERROR);
6✔
38
    setOutOfMemoryErrorHandlerForMemory(myFatalError);
6✔
39
    setInternalCheckFailHandlerForMemory(myInternalCheckFailed);
6✔
40
}
6✔
41
AfterEach(Memory) {}
6✔
42

43

44
#define SIZE_testMemory 12
45

46
Memory testMemory;
47

48
Ensure(Memory, can_allocate_memory) {
1✔
49
    memoryInit(&testMemory, "", SIZE_testMemory);
1✔
50

51
    char *pointer = memoryAllocc(&testMemory, 1, sizeof(char));
1✔
52
    assert_that(pointer, is_not_null);
1✔
53
    assert_that(pointer, is_equal_to(testMemory.area)); /* First allocated item in testMemory */
1✔
54
}
1✔
55

56
Ensure(Memory, will_fatal_on_overflow_in_memory) {
1✔
57
    memoryInit(&testMemory, "", SIZE_testMemory);
1✔
58

59
    /* Allocate more that size renders fatalError() */
60
    fatalErrorAllowed = true;
1✔
61
    memoryAllocc(&testMemory, SIZE_testMemory + 1, sizeof(char));
1✔
62
    assert_that(fatalErrorCalled);
1✔
63
}
1✔
64

65
Ensure(Memory, can_free_until_in_memory) {
1✔
66
    memoryInit(&testMemory, "", SIZE_testMemory);
1✔
67

68
    /* Allocate some memory */
69
    void *pointer1 = memoryAlloc(&testMemory, 2);
1✔
70
    assert_that(testMemory.index, is_not_equal_to(0));
1✔
71

72
    memoryAlloc(&testMemory, 4);
1✔
73

74
    memoryFreeUntil(&testMemory, pointer1);
1✔
75
    assert_that(testMemory.index, is_equal_to(0));
1✔
76
}
1✔
77

78
Ensure(Memory, can_realloc_in_memory) {
1✔
79
    int initialSize = 2;
1✔
80
    int newSize = 4;
1✔
81

82
    memoryInit(&testMemory, "", SIZE_testMemory);
1✔
83

84
    /* Allocate some memory */
85
    void *pointer1 = memoryAlloc(&testMemory, initialSize);
1✔
86
    assert_that(testMemory.index, is_equal_to(initialSize));
1✔
87

88
    void *pointer2 = memoryRealloc(&testMemory, pointer1, initialSize, newSize);
1✔
89
    assert_that(pointer2, is_equal_to(pointer1));
1✔
90
    assert_that(testMemory.index, is_equal_to(newSize));
1✔
91
}
1✔
92

93
Ensure(Memory, should_fatal_if_freeing_not_in_memory) {
1✔
94
    memoryInit(&testMemory, "", SIZE_testMemory);
1✔
95

96
    void *pointer = &testMemory.area+1;
1✔
97

98
    internalCheckFailAllowed = true;
1✔
99
    if (setjmp(internalCheckJmpbuf) == 0)
2✔
100
        memoryFreeUntil(&testMemory, pointer);
1✔
101

102
    assert_that(internalCheckFailCalled);
1✔
103
}
1✔
104

105
Ensure(Memory, should_fatal_if_reallocing_not_last_allocated) {
1✔
106
    memoryInit(&testMemory, "", SIZE_testMemory);
1✔
107

108
    void *pointer = memoryAlloc(&testMemory, 5);
1✔
109

110
    fatalErrorAllowed = true;
1✔
111
    internalCheckFailAllowed = true;
1✔
112
    if (setjmp(internalCheckJmpbuf) == 0)
2✔
113
        memoryRealloc(&testMemory, pointer+1, 5, 6);
1✔
114

115
    assert_that(internalCheckFailCalled);
1✔
116
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc