< #if !defined(BOOL_H) --- > #ifndef BOOL_H
> > #ifndef FALSE
> #endif > > #ifndef TRUE
> #endif >
> > /* > ** bool_initMod has no real effect > ** Declared with modifies internalState, so no noeffect errors are > ** reported when it is called.) > */ > > extern /*@unused@*/ void bool_initMod (void) /*@modifies internalState@*/ ; > /*@-mustmod@*/
> /*@=mustmod@*/ > > extern /*@unused@*/ /*@observer@*/ char *bool_unparse (bool b) /*@*/ ; > # define bool_unparse(b) ((b) ? "true" : "false" ) > > extern /*@unused@*/ bool bool_not (bool b) /*@*/ ; > # define bool_not(b) ((b) ? FALSE : TRUE) > > extern /*@unused@*/ bool bool_equal (bool b1, bool b2) /*@*/ ; > # define bool_equal(a,b) ((a) ? (b) : !(b)) >
< typedef enum {mMGRS, fMGRS, mNON, fNON} employeeKinds; --- > typedef enum > { > mMGRS, fMGRS, mNON, fNON > } employeeKinds;
< void db_initMod(void) { --- > void db_initMod (void) > {
< if (initDone) return; --- > > if (initDone) > { > return; > } >
>
> {
> } >
< eref _db_ercKeyGet(erc c, int key) { --- > eref _db_ercKeyGet (erc c, int key) > {
>
< if (eref_get(er).ssNum == key) erc_iterReturn(it, er); --- > { > if (eref_get (er).ssNum == key) > { > erc_iterReturn (it, er); > } > } >
< < eref _db_keyGet(int key) { --- > eref _db_keyGet (int key) > {
< for (i = firstERC; i <= lastERC; i++) { --- > > for (i = firstERC; i <= lastERC; i++) > {
< if (!eref_equal(er, erefNIL)) return er; --- > if (!eref_equal (er, erefNIL)) > { > return er;
> } >
< int _db_addEmpls(erc c, int l, int h, empset s) { --- > int _db_addEmpls (erc c, int l, int h, empset s) > {
< for_ercElems (er, it, c) { --- > > for_ercElems (er, it, c) > {
< if ((e.salary >= l) && (e.salary <= h)) { --- > if ((e.salary >= l) && (e.salary <= h)) > {
>
< db_status hire(employee e) { < if (e.gen == gender_ANY) return genderERR; < if (e.j == job_ANY) return jobERR; < if (e.salary < 0) return salERR; --- > db_status hire (employee e) > { > if (e.gen == gender_ANY) > return genderERR; > > if (e.j == job_ANY) > return jobERR; > > if (e.salary < 0) > return salERR; >
>
< void uncheckedHire(employee e) { --- > void uncheckedHire (employee e) > {
>
>
< else erc_insert(db[mNON], er); < else if (e.j == MGR) --- > else > erc_insert (db[mNON], er); > else > if (e.j == MGR)
< else erc_insert(db[fNON], er); --- > else > erc_insert (db[fNON], er);
< bool fire(int ssNum) { --- > bool fire (int ssNum) > {
>
< if (eref_get(er).ssNum == ssNum) { --- > if (eref_get (er).ssNum == ssNum) > {
>
< bool promote(int ssNum) { --- > bool promote (int ssNum) > {
>
< if (eref_equal(er, erefNIL)) { --- > > if (eref_equal (er, erefNIL)) > {
< if (eref_equal(er, erefNIL)) return FALSE; --- > if (eref_equal (er, erefNIL)) > return FALSE;
>
< if (g == MALE) { --- > > if (g == MALE) > {
< else { --- > else > {
>
< db_status setSalary(int ssNum, int sal) { --- > db_status setSalary (int ssNum, int sal) > {
< if (sal < 0) return salERR; --- > > if (sal < 0) > { > return salERR; > } >
< if (eref_equal(er, erefNIL)) return missERR; --- > > if (eref_equal (er, erefNIL)) > { > return missERR; > } >
>
< int query(db_q q, empset s) { --- > int query (db_q q, empset s) > {
>
< switch(q.g) { --- > > switch (q.g) > {
< switch(q.j) { --- > switch (q.j) > {
< switch(q.j) { --- > switch (q.j) > {
< switch(q.j) { --- > switch (q.j) > {
< void db_print(void) { --- > void db_print (void) > {
>
< for (i = firstERC; i <= lastERC; i++) { --- > > for (i = firstERC; i <= lastERC; i++) > {
< #if !defined(DBASE_H) --- > # ifndef DBASE_H
< imports employee, empset, stdio; --- > imports employee, empset, <stdio>;
< uses dbase, sprint(ioStream, db); < < claims UniqueKeys(employee e1, employee e2) db d; { < ensures --- > claims UniqueKeys (employee e1, employee e2) db d; > { > /* ensures
> */
< db_status hire(employee e) db d; { --- > db_status hire(employee e) db d; > {
< ensures --- > /* ensures
> */
< void uncheckedHire(employee e) db d; { < requires e.gen \neq gender_ANY /\ e.j \neq job_ANY --- > > void uncheckedHire(employee e) db d; > { > /* requires e.gen \neq gender_ANY /\ e.j \neq job_ANY
> */
< ensures d' = hire(e, d^); --- > /* ensures d' = hire(e, d^); */
< bool fire(int ssNum) db d; { --- > > bool fire(int ssNum) db d; > {
< ensures result = employed(d^, ssNum) --- > /* ensures result = employed(d^, ssNum)
> */
< int query(db_q q, empset s) db d; { --- > > int query(db_q q, empset s) db d; > {
< ensures s' = s^ \U query(d^, q) --- > /* ensures s' = s^ \U query(d^, q)
> */
< bool promote(int ssNum) db d; { --- > > bool promote(int ssNum) db d; > {
< ensures --- > /* ensures
> */
< db_status setSalary(int ssNum, int sal) db d; { --- > > db_status setSalary(int ssNum, int sal) db d; > {
> /*
> */
< void db_print(void) db d; FILE *stdout; { --- > > void db_print(void) db d; FILE *stdout; > {
> /*
< --- > */
< void db_initMod(void) db d; { --- > > void db_initMod(void) db d; > {
< ensures d' = new; --- > /* ensures d' = new; */
< int main(int argc, char *argv[]) { < --- > int main(int argc, char *argv[]) > {
< if (argc != 1) { --- > if (argc != 1) > {
< if (!(empset_size(em1) == 0)) printf("Size should be 0.\n"); < for (i = 0; i < 500; i++) { --- > > if (!(empset_size(em1) == 0)) > { > printf("Size should be 0.\n"); > } > > for (i = 0; i < 500; i++) > {
< if (!(empset_size(em1) == 500)) printf("Size should be 500.\n"); < for (i = 0; i < 250; i++) { --- > > if (!(empset_size(em1) == 500)) > { > printf("Size should be 500.\n"); > } > > for (i = 0; i < 250; i++) > {
< if (!(empset_size(em1) == 250)) printf("Size should be 250.\n"); --- > > if (!(empset_size(em1) == 250)) > { > printf("Size should be 250.\n"); > } >
< for (i = 0; i < 100; i++) { --- > > for (i = 0; i < 100; i++) > {
>
< if (!(empset_size(em3) == 350)) printf("Size should be 350.\n"); --- > > if (!(empset_size(em3) == 350)) > { > printf("Size should be 350.\n"); > } >
< if (!(empset_size(em3) == 350)) printf("Size should be 350.\n"); --- > > if (!(empset_size(em3) == 350)) > { > printf("Size should be 350.\n"); > } >
< for (i = 0; i < 2; i++) { --- > > for (i = 0; i < 2; i++) > {
< <
< for (i = 0; i < 20; i++) { --- > > for (i = 0; i < 20; i++) > {
< if ( (i/2)*2 == i) hire(e); < else {uncheckedHire(e); j = hire(e);} --- > > if ((i/2)*2 == i) > { > hire(e);
> else > { > uncheckedHire(e); j = hire(e); > } > } >
>
>
>
>
>
> # include <string.h>
< bool employee_setName(employee *e, char na []) { --- > bool employee_setName (employee *e, char na []) > {
> {
> } >
< bool employee_equal(employee * e1, employee * e2) { --- > > bool employee_equal (employee * e1, employee * e2) > {
< && (strncmp(e1->name, e2->name, < maxEmployeeName) == 0)); --- > && (strncmp (e1->name, e2->name, maxEmployeeName) == 0));
< void employee_sprint(char s[], employee e) { --- > > void employee_sprint (char s[], employee e) > {
< (void) sprintf(s, < employeeFormat, < e.ssNum, < e.name, < gender[e.gen], < jobs[e.j], < e.salary); --- > (void) sprintf (s, employeeFormat, e.ssNum, e.name, > gender[e.gen], jobs[e.j], e.salary);
< #if !defined(EMPLOYEE_H) --- > # ifndef EMPLOYEE_H
< typedef struct {int ssNum; --- > > typedef struct > { > int ssNum;
< job j;} employee; --- > job j; > } employee;
< uses employeeConstants, sprint(employee, char[]); < < void employee_sprint(char s[], employee e) { < requires maxIndex(s) >= employeePrintSize; --- > void employee_sprint (out char s[], employee e) > { > /* requires maxIndex(s) >= employeePrintSize; */
< ensures isSprint(s', e) --- > /* ensures isSprint(s', e)
> */
< bool employee_equal(employee *e1, employee *e2) { < ensures result = sameStr(e1->name^, e2->name^) --- > > bool employee_equal (employee *e1, employee *e2) > { > /* ensures result = sameStr(e1->name^, e2->name^)
> */
< bool employee_setName(employee *e, char na[]) { < requires nullTerminated(na^); --- > > bool employee_setName(employee *e, char na[]) > { > /* requires nullTerminated(na^); */
< ensures result = lenStr(na^) < maxEmployeeName --- > /* ensures result = lenStr(na^) < maxEmployeeName
> */
< void employee_initMod(void) { --- > > void employee_initMod(void) > {
< eref _empset_get(employee e, erc s) { --- > eref _empset_get (employee e, erc s) > {
< for_ercElems(er, it, s) { --- > > for_ercElems (er, it, s) > {
>
< void empset_clear(empset s) { --- > void empset_clear (empset s) > {
< bool empset_insert(empset s, employee e) { --- > bool empset_insert (empset s, employee e) > {
< if (!eref_equal(_empset_get(e, s), erefNIL)) return FALSE; --- > > if (!eref_equal (_empset_get (e, s), erefNIL)) > { > return FALSE; > } >
< void empset_insertUnique(empset s, employee e) { --- > void empset_insertUnique (empset s, employee e) > {
>
< if (eref_equal(er, erefNIL)) { --- > > if (eref_equal (er, erefNIL)) > {
>
< bool empset_delete(empset s, employee e) { --- > bool empset_delete (empset s, employee e) > {
>
< if (eref_equal(er, erefNIL)) return FALSE; --- > > if (eref_equal (er, erefNIL)) > { > return FALSE; > }
< empset empset_disjointUnion(empset s1, empset s2) { --- > empset empset_disjointUnion (empset s1, empset s2) > {
>
< if (erc_size(s1) > erc_size(s2)) { --- > > if (erc_size (s1) > erc_size (s2)) > {
>
>
< empset empset_union(empset s1, empset s2) { --- > empset empset_union (empset s1, empset s2) > {
>
< if (erc_size(s1) > erc_size(s2)) { --- > > if (erc_size (s1) > erc_size (s2)) > {
>
>
< void empset_intersect(empset s1, empset s2) { --- > void empset_intersect (empset s1, empset s2) > {
>
>
>
>
< bool empset_subset(empset s1, empset s2) { --- > bool empset_subset (empset s1, empset s2) > {
>
< void empset_initMod(void) { --- > void empset_initMod (void) > {
>
< #if !defined(EMPSET_H) --- > # ifndef EMPSET_H
>
< uses Set(employee, empset), < sprint(empset, char[]);
< empset empset_create(void) { < ensures fresh(result) /\ result' = { }; --- > empset empset_create(void) > { > /* ensures fresh(result) /\ result' = { }; */
< void empset_final(empset s) { --- > > void empset_final(empset s) > {
< ensures trashed(s); --- > /* ensures trashed(s); */
< void empset_clear(empset s) { --- > > void empset_clear(empset s) > {
< ensures s' = { }; --- > /* ensures s' = { }; */
< bool empset_insert(empset s, employee e) { --- > > bool empset_insert(empset s, employee e) > {
< ensures result = ~(e \in s^) /\ s' = insert(e, s^); --- > /* ensures result = ~(e \in s^) /\ s' = insert(e, s^); */
< void empset_insertUnique(empset s, employee e) { < requires ~(e \in s^); --- > > void empset_insertUnique(empset s, employee e) > { > /* requires ~(e \in s^); */
< ensures s' = insert(e, s^); --- > /* ensures s' = insert(e, s^); */
< bool empset_delete(empset s, employee e) { --- > > bool empset_delete(empset s, employee e) > {
< ensures result = e \in s^ /\ s' = delete(e, s^); --- > /* ensures result = e \in s^ /\ s' = delete(e, s^); */
< empset empset_union(empset s1, empset s2) { < ensures result' = s1^ \U s2^ /\ fresh(result); --- > > empset empset_union(empset s1, empset s2) > { > /* ensures result' = s1^ \U s2^ /\ fresh(result); */
< empset empset_disjointUnion(empset s1, empset s2) { < requires s1^ \I s2^ = { }; < ensures result' = s1^ \U s2^ /\ fresh(result); --- > > empset empset_disjointUnion(empset s1, empset s2) > { > /* requires s1^ \I s2^ = { }; */ > /* ensures result' = s1^ \U s2^ /\ fresh(result); */
< void empset_intersect(empset s1, empset s2) { --- > > void empset_intersect(empset s1, empset s2) > {
< ensures s1' = s1^ \I s2^; --- > /* ensures s1' = s1^ \I s2^; */
< int empset_size(empset s) { < ensures result = size(s^); --- > > int empset_size(empset s) > { > /* ensures result = size(s^); */
< bool empset_member(employee e, empset s) { < ensures result = e \in s^; --- > > bool empset_member(employee e, empset s) > { > /* ensures result = e \in s^; */
< bool empset_subset(empset s1, empset s2) { < ensures result = s1^ \subseteq s2^; --- > > bool empset_subset(empset s1, empset s2) > { > /* ensures result = s1^ \subseteq s2^; */
< employee empset_choose(empset s) { < requires s^ \neq { }; < ensures result \in s^; --- > > employee empset_choose(empset s) > { > /* requires s^ \neq { }; */ > /* ensures result \in s^; */
< char *empset_sprint(empset s) { < ensures isSprint(result[]', s^) /\ fresh(result[]); --- > > char *empset_sprint(empset s) > { > /* ensures isSprint(result[]', s^) /\ fresh(result[]); */
< void empset_initMod(void) { --- > > void empset_initMod(void) > {
> # include <stdlib.h>
< erc erc_create(void) { --- > erc erc_create (void) > {
>
< if (c == 0) { --- > > if (c == 0) > {
>
< void erc_clear(erc c) { --- > void erc_clear (erc c) > {
< for (elem = c->vals; elem != 0; elem = next) { --- > > for (elem = c->vals; elem != 0; elem = next) > {
>
< void erc_final(erc c) { --- > void erc_final (erc c) > {
< bool erc_member(eref er, erc c) { --- > bool erc_member (eref er, erc c) > {
>
>
< void erc_insert(erc c, eref er) { --- > void erc_insert (erc c, eref er) > {
< if (newElem == 0) { --- > > if (newElem == 0) > {
>
< bool erc_delete(erc c, eref er) { --- > bool erc_delete (erc c, eref er) > {
< prev = elem, elem = elem->next) { < if (elem->val == er) { --- > prev = elem, elem = elem->next) > { > if (elem->val == er) > {
< else {prev->next = elem->next;} --- > else > prev->next = elem->next; >
>
< ercIter erc_iterStart(erc c) { --- > ercIter erc_iterStart (erc c) > {
>
< if (result == 0) { --- > > if (result == 0) > {
>
< eref erc_yield(ercIter it) { --- > eref erc_yield (ercIter it) > {
< if (*it == 0) { --- > > if (*it == 0) > {
>
< void erc_join(erc c1, erc c2) { --- > void erc_join (erc c1, erc c2) > {
>
< char * erc_sprint(erc c) { --- > char *erc_sprint (erc c) > {
< result = (char*)malloc(erc_size(c) < *(employeePrintSize+1)+1); < if (result == 0) { --- > > result = (char *) > malloc (erc_size (c) * (employeePrintSize + 1) + 1); > > if (result == 0) > {
>
< for_ercElems (er, it, c) { --- > > for_ercElems (er, it, c) > {
>
< #if !defined(ERC_H) --- > # ifndef ERC_H
>
>
>
>
< uses erc(obj erc for ercObj), sprint(erc, char[]); < < erc erc_create(void) { < ensures fresh(result) /\ result' = { }; --- > erc erc_create(void) > { > /* ensures fresh(result) /\ result' = { }; */
< void erc_clear(erc c) { < requires c^.activeIters = 0; --- > > void erc_clear(erc c) > { > /* requires c^.activeIters = 0; */
< ensures c' = { }; --- > /* ensures c' = { }; */
< void erc_insert(erc c, eref er) { < requires c^.activeIters = 0 /\ er \neq erefNIL; --- > > void erc_insert(erc c, eref er) > { > /* requires c^.activeIters = 0 /\ er \neq erefNIL; */
< ensures c' = [insert(er, c^.val), 0]; --- > /* ensures c' = [insert(er, c^.val), 0]; */
< bool erc_delete(erc c, eref er) { < requires c^.activeIters = 0; --- > > bool erc_delete(erc c, eref er) > { > /* requires c^.activeIters = 0; */
< ensures result = er \in c^.val < /\ c' = [delete(er, c^.val), 0]; --- > /* ensures result = er \in c^.val > /\ c' = [delete(er, c^.val), 0]; */
< bool erc_member(eref er, erc c) { < ensures result = er \in c^.val; --- > > bool erc_member(eref er, erc c) > { > /* ensures result = er \in c^.val; */
< eref erc_choose(erc c) { < requires size(c^.val) \neq 0; < ensures result \in c^.val; --- > > eref erc_choose(erc c) > { > /* requires size(c^.val) \neq 0; */ > /* ensures result \in c^.val; */
< int erc_size(erc c) { < ensures result = size(c^.val); --- > > int erc_size(erc c) > { > /* ensures result = size(c^.val); */
< ercIter erc_iterStart(erc c) { --- > > ercIter erc_iterStart(erc c) > {
< ensures fresh(result) /\ result' = [c^.val, c] --- > /* ensures fresh(result) /\ result' = [c^.val, c]
> */
< eref erc_yield(ercIter it) { < modifies it, it^.eObj; < ensures if it^.toYield \neq { } --- > > eref erc_yield(ercIter it) > { > modifies it; /* , it^.eObj */ > /* ensures if it^.toYield \neq { }
> */
< void erc_iterFinal(ercIter it) { < modifies it, it^.eObj; < ensures trashed(it) --- > > void erc_iterFinal(ercIter it) > { > modifies it; /* , it^.eObj; */ > /* ensures trashed(it)
> */
< void erc_join(erc c1, erc c2) { < requires c1^.activeIters = 0; --- > > void erc_join(erc c1, erc c2) > { > /* requires c1^.activeIters = 0; */
< ensures c1' = [c1^.val \U c2^.val, 0]; --- > /* ensures c1' = [c1^.val \U c2^.val, 0]; */
< char *erc_sprint(erc c) { < ensures isSprint(result[]', c^) /\ fresh(result[]); --- > > char *erc_sprint(erc c) > { > /* ensures isSprint(result[]', c^) /\ fresh(result[]); */
< void erc_final(erc c) { --- > > void erc_final(erc c) > {
< ensures trashed(c); --- > /* ensures trashed(c); */
< void erc_initMod(void) { --- > > void erc_initMod(void) > {
> # include <stdlib.h>
< eref eref_alloc(void) { --- > eref eref_alloc (void) > {
< for (i=0; < (eref_Pool.status[i] == used) < && (i < eref_Pool.size); < i++); --- > for (i=0; (eref_Pool.status[i] == used) && (i < eref_Pool.size); i++); >
< if (res == eref_Pool.size) { --- > > if (res == eref_Pool.size) > {
< if (eref_Pool.conts == 0) { --- > if (eref_Pool.conts == 0) > {
>
< if (eref_Pool.status == 0) { --- > > if (eref_Pool.status == 0) > {
>
>
>
< void eref_initMod(void) { --- > > void eref_initMod (void) > {
< if (needsInit == FALSE) return; --- > if (needsInit == FALSE) > { > return; > } >
< eref_Pool.conts = < (employee *) malloc(size*sizeof(employee)); < if (eref_Pool.conts == 0) { --- > > eref_Pool.conts = (employee *) malloc (size * sizeof (employee)); > > if (eref_Pool.conts == 0) > {
< eref_Pool.status = < (eref_status *) malloc(size*sizeof(eref_status)); < if (eref_Pool.status == 0) { --- > > eref_Pool.status = (eref_status *) malloc (size * sizeof (eref_status)); > > if (eref_Pool.status == 0) > {
>
< for (i = 0; i < size; i++) eref_Pool.status[i] = avail; --- > > for (i = 0; i < size; i++) > { > eref_Pool.status[i] = avail; > }
< #if !defined(EREF_H) --- > # ifndef EREF_H
< typedef struct {employee *conts; --- > typedef struct { > employee *conts;
< int size;} eref_ERP; --- > int size; > } eref_ERP;
>
>
< uses refTable(eref, employee, map); <
< constant eref erefNIL = nil; --- > constant eref erefNIL;
< eref eref_alloc(void) map m; { --- > eref eref_alloc(void) map m; > {
< ensures newInd(result, m^, m'); --- > /* ensures newInd(result, m^, m'); */
< void eref_free(eref er) map m; { < requires er \in domain(m^); --- > > void eref_free(eref er) map m; > { > /* requires er \in domain(m^); */
< ensures m' = delete(m^, er); --- > /* ensures m' = delete(m^, er); */
< void eref_assign(eref er, employee e) map m; { < requires er \in domain(m^); --- > > void eref_assign(eref er, employee e) map m; > { > /* requires er \in domain(m^); */
< ensures m' = assign(m^, er, e); --- > /* ensures m' = assign(m^, er, e); */
< employee eref_get(eref er) map m; { < requires er \in domain(m^); < ensures result = m^[er]; --- > > employee eref_get(eref er) map m; > { > /* requires er \in domain(m^); */ > /* ensures result = m^[er]; */
< bool eref_equal(eref er1, eref er2) { < ensures result = (er1 = er2); --- > > bool eref_equal(eref er1, eref er2) > { > /* ensures result = (er1 = er2); */
< void eref_initMod(void) map m; { --- > > void eref_initMod(void) map m; > {
< ensures m' = new; --- > /* ensures m' = new; */
< /* ereftab.c */ --- > /* > ** This is not a good implementation. I should probably replace > ** the erc with a hash table. > */
< /* This is not a good implementation. I should probably replace < the erc with a hash table. */ <
< ereftab ereftab_create(void) { --- > ereftab ereftab_create (void) > {
< void ereftab_insert(ereftab t, employee e, eref er) { --- > void ereftab_insert (ereftab t, employee e, eref er) > {
< bool ereftab_delete(ereftab t, eref er) { --- > bool ereftab_delete (ereftab t, eref er) > {
< eref ereftab_lookup(employee e, ereftab t) { --- > eref ereftab_lookup (employee e, ereftab t) > {
< for_ercElems(er, it, t) { --- > for_ercElems (er, it, t) > {
>
< void ereftab_initMod(void) { --- > void ereftab_initMod (void) > {
< #if !defined(EREFTAB_H) --- > # ifndef EREFTAB_H
< uses ereftab; < < ereftab ereftab_create(void) { < ensures result' = empty; --- > ereftab ereftab_create(void) > { > /* ensures result' = empty; */
< void ereftab_insert(ereftab t, employee e, eref er) { < requires getERef(t^, e) = erefNIL; --- > > void ereftab_insert(ereftab t, employee e, eref er) > { > /* requires getERef(t^, e) = erefNIL; */
< ensures t' = add(t^, e, er); --- > /* ensures t' = add(t^, e, er); */
< bool ereftab_delete(ereftab t, eref er) { --- > > bool ereftab_delete(ereftab t, eref er) > {
< ensures result = in(t^, er) /\ t' = delete(t^, er); --- > /* ensures result = in(t^, er) /\ t' = delete(t^, er); */
< eref ereftab_lookup(employee e, ereftab t) { < ensures result = getERef(t^, e); --- > > eref ereftab_lookup(employee e, ereftab t) > { > /* ensures result = getERef(t^, e); */
< void ereftab_initMod(void) { --- > > void ereftab_initMod(void) > {