File: pointer_to_array_sensitivity_tests.c

package info (click to toggle)
cbmc 5.10-5
  • links: PTS
  • area: main
  • in suites: buster
  • size: 73,416 kB
  • sloc: cpp: 264,330; ansic: 38,268; java: 19,025; python: 4,539; yacc: 4,275; makefile: 2,547; lex: 2,394; sh: 932; perl: 525; xml: 289; pascal: 169
file content (59 lines) | stat: -rw-r--r-- 1,081 bytes parent folder | download | duplicates (2)
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
#include <assert.h>
#include <stddef.h>

int main(int argc, char *argv[])
{
  // Test reading from an array using a pointer
  int a[3]={1, 2, 3};
  int *p=a;
  assert(p==&a[0]);
  assert(*p==1);

  // Test pointer arithmetic
  int *q=&a[1];
  assert(q==p+1);
  assert(*q==2);

  // Test pointer diffs
  ptrdiff_t x=1;
  assert(q-p==x);

  // Test writing into an array using a pointer
  *q=4;
  assert(a[1]==4);
  a[1]=2;

  // We now explore pointers and indexes each with more than one possible value
  int *r=&a[1];
  int b[3]={0, 0, 0};
  int *s=&b[1];
  int i=1;
  if (argc>2)
  {
    r=&a[2];
    s=&b[2];
    i=2;
  }

  // Test reading from an array using a pointer with more than one possible
  // value
  assert(*r==2);
  assert(*r==1);
  assert(*s==0);
  assert(*s==1);

  // Test pointer arithmetic with an unknown index
  int *t=&a[i];
  assert(t==p+i);

  // Test pointer diffs with an unknown index
  ptrdiff_t y=i;
  assert(t-p==y);

  // Test writing into an array using a pointer with an unknown index
  *r=5;
  assert(a[i]==5);
  assert(a[1]==5);

  return 0;
}