source: git/ppcc/adlib/test1.cc @ 54b24c

spielwiese
Last change on this file since 54b24c was 54b24c, checked in by Reimer Behrends <behrends@…>, 5 years ago
Finalizing thread support.
  • Property mode set to 100644
File size: 1.3 KB
Line 
1#include "lib.h"
2
3#define CMD "seq"
4#define ARGS "10000"
5
6struct Node : public GC {
7  Node *left, *right;
8  Int val;
9  Word dummy[16 - 3];
10};
11
12Node *make_tree(int depth, Int &counter) {
13  Node *node = new Node();
14  if (depth != 0) {
15    node->left = make_tree(depth - 1, counter);
16    node->val = counter++;
17    node->right = make_tree(depth - 1, counter);
18  } else {
19    node->val = counter++;
20  }
21  return node;
22}
23
24Int sum_tree(Node *node) {
25  Int result = 0;
26  if (node) {
27    result += node->val;
28    result += sum_tree(node->left);
29    result += sum_tree(node->right);
30  }
31  return result;
32}
33
34static const int iter = 20;
35static const int max_depth = 15; // 2^16-1 nodes.
36
37void Main() {
38  Str *cmd = new Str(CMD);
39  StrArr *args = (new Str(ARGS))->split(' ');
40  Int n = 0;
41  Str *output;
42  for (int j = 0; j < iter; j++) {
43    output = ReadProcess(cmd, args);
44    n += output->len();
45  }
46  Check(n == iter * 48894, "read process output as bytes");
47  Node *tree;
48  static Node *permtree;
49  Int counter;
50  counter = 0;
51  GCVar(permtree, make_tree(max_depth, counter));
52  for (int i = 0; i < iter; i++) {
53    counter = 0;
54    tree = make_tree(max_depth, counter);
55  }
56  // This may overflow on 32-bit machines, so we use unsigned
57  // modulo arithmetic without division.
58  Check(sum_tree(tree) * 2 == counter * (counter - 1),
59      "stress test memory allocation");
60}
Note: See TracBrowser for help on using the repository browser.