-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_generic_test.c
73 lines (62 loc) · 1.79 KB
/
list_generic_test.c
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
/****************************************************************
* Singly linked generic list
*
* Copyright Alastair Reid 2020
* SPDX-Licence-Identifier: BSD-3-Clause
****************************************************************/
#include "stdlib.h"
#include "list_generic.h"
struct T {
int value;
};
//@ predicate T(struct T* t;) = malloc_block_T(t) &*& t->value |-> ?v &*& v >= 0;
//@ predicate_family_instance Ownership(freeT)(struct T* t) = T(t);
struct T* mkT(int x)
//@ requires x >= 0;
//@ ensures Ownership(freeT)(result);
{
struct T* r = malloc(sizeof(struct T));
if (r == 0) abort();
r->value = x;
//@ close Ownership(freeT)(r);
return r;
}
void freeT(void* d) //@ : destructor
//@ requires Ownership(freeT)(d);
//@ ensures true;
{
//@ open Ownership(freeT)(d);
struct T* t = (struct T*)d;
//@ open T(t);
free(t);
}
void test_list()
//@ requires true;
//@ ensures true;
{
struct node *l = 0;
//@ close list(l, freeT);
l = cons(mkT(3), l);
l = cons(mkT(4), l);
if (!l) abort(); // we don't track size of list but head/tail require non-empty list
struct T* x = head(&l);
if (!l) abort(); // we don't track size of list but head/tail require non-empty list
struct T* y = head(&l);
// we don't track contents of list but we know that T(_) must hold for all elements
//@ open Ownership(freeT)(x);
//@ open Ownership(freeT)(y);
//@ open T(x);
//@ open T(y);
assert(x->value >= 0);
assert(y->value >= 0);
//@ close Ownership(freeT)(x);
//@ close Ownership(freeT)(y);
freeT(x);
freeT(y);
// we don't track the size of a list so we cannot show that the list is empty so
// we still have to dispose of it.
list_dispose(l, freeT);
}
/****************************************************************
* End
****************************************************************/