tests.c 2.16 KB
Newer Older
Michael Sammler's avatar
Michael Sammler committed
1
2
#include <stddef.h>
#include <stdbool.h>
3
#include <stdint.h>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Random tests.

[[rc::returns("void")]]
void test1(){
  long long ll = 0;
  long l = 0;
  int i = 0;
  short s = 0;
  char c = 0;

  if(ll == l) return;
  if(ll == l) return;
  if(ll == i) return;
  if(ll == s) return;
  if(ll == c) return;

  if(!ll) return;
  if(!l) return;
  if(!i) return;
  if(!s) return;
  if(!c) return;

  return;
}
Michael Sammler's avatar
Michael Sammler committed
28

29
30
31
32
33
34
35
36
37
38
[[rc::returns("{1} @ int<i32>")]]
int return1(){
  return 1;
}

[[rc::requires("False")]]
int unreachable(){
  assert(1 == 2);
}

Michael Sammler's avatar
Michael Sammler committed
39
40
41
42
[[rc::returns("void")]]
void test_ternary(){
  int local = 0;
  assert((2 ? 3 : 2) == 3);
43
  assert((&local != NULL ? (return1() ? return1() : unreachable()) + 3 : 2) == 4);
Michael Sammler's avatar
Michael Sammler committed
44
}
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

[[rc::returns("void")]]
void test_bits() {
  // resource: https://en.cppreference.com/w/c/language/operator_arithmetic
  unsigned int mask = 0x00f0; // try uint16_t when issue #34 is fixed
  unsigned int a = 0x12345678;
  unsigned int setting_bits = a | mask;
  unsigned int selecting_bits = a & mask;
  unsigned int clearing_bits = a & ~mask;
  assert(setting_bits == 0x123456f8);
  assert(selecting_bits == 0x70);
  assert(clearing_bits == 0x12345608);

  // bitwise not on signed integers
  assert(~(-2) == 1);
  assert(~0 == -1);
}
62
63
64
65
66
67
68

[[rc::returns("{0} @ int<i32>")]]
int test_comma(){
  int x = (NULL, 42);

  return x + x, x - x;
}
69
70
71
72
73
74
75
76
77
78
79
80
81
82

[[rc::global("{1} @ int<i32>")]]
int global;

[[rc::inlined]]
int inline_global1() {
  return global;
}

[[rc::requires("[initialized \"global\" ()]")]]
[[rc::returns("{1} @ int<i32>")]]
int inline_global2() {
  return inline_global1();
}
83
84
85
86
87
88
89
90
91
92
93
94

[[rc::returns("void")]]
void test_logical(){
  if ((1 && 2) || 0/0) {
    assert(1);
  } else {
    assert(0);
  }
  assert((1 && 2) == 1 && (2 || 0) == 1);
  if(INT_MAX) { assert(1); } else { assert(0); }
  assert(UINT_MAX);
}
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

[[rc::returns("void")]]
void test_not_ptr(){
  int i;
  int *pi = &i;

  if(!pi){
    assert(0);
  }

  if(pi){
    assert(1);
  } else {
    assert(0);
  }

  assert(pi);

  void *p = NULL;
  if(p){
    assert(0);
  }

  if(!p){
    assert(1);
  } else {
    assert(0);
  }

  assert(!p);
}
126
127
128
129
130

[[rc::returns("{0} @ int<i32>")]]
int main(){
  // Check that [return 0] is inserted corectly.
}