summaryrefslogtreecommitdiff
path: root/vere/pkg/noun/life_test.c
blob: cffe97bd7b417db854ac28eb8bfa11598098d856 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/// @file
/// Test lifecycle formula on different subjects

#include "noun.h"

/* _setup(): prepare for tests.
*/
static void
_setup(void)
{
  u3m_boot_lite(1 << 28);  // 256MB loom
}

/* Helper to run nock with subject+formula in a cell for u3m_soft */
static u3_noun
_nock_pair(u3_noun sub_fol)
{
  u3_noun sub, fol;
  u3x_cell(sub_fol, &sub, &fol);
  return u3n_nock_on(u3k(sub), u3k(fol));
}

/* _test_life_on_null(): test lifecycle formula on atom 0
*/
static c3_i
_test_life_on_null(void)
{
  fprintf(stderr, "\n=================================================\n");
  fprintf(stderr, "Testing lifecycle formula [2 [0 3] [0 2]] on null\n");
  fprintf(stderr, "=================================================\n\n");

  /* Build lifecycle formula: [2 [0 3] [0 2]] */
  u3_noun lyf = u3nt(2, u3nc(0, 3), u3nc(0, 2));

  fprintf(stderr, "Formula: [2 [0 3] [0 2]]\n");
  fprintf(stderr, "Subject: 0 (null)\n");
  fprintf(stderr, "Formula mug: %x\n\n", u3r_mug(lyf));

  /* Test on null (atom 0) */
  u3_noun eve = 0;  /* u3_nul is defined as 0 */

  fprintf(stderr, "Testing: *[0 [2 [0 3] [0 2]]]\n\n");

  /* Try running it - need to pass [subject formula] as single argument */
  u3_noun pair = u3nc(eve, lyf);
  u3_noun pro = u3m_soft(0, _nock_pair, pair);

  if ( u3_blip != u3h(pro) ) {
    fprintf(stderr, "✗ FAILED!\n");
    fprintf(stderr, "Error: ");
    u3m_p("", u3h(pro));
    fprintf(stderr, "\n");

    /* Print trace if available */
    if ( u3du(pro) ) {
      fprintf(stderr, "\nTrace:\n");
      u3m_p("trace", u3t(pro));
      fprintf(stderr, "\n");
    }

    fprintf(stderr, "\nThis confirms the formula CANNOT work on atom 0!\n");
    u3z(pro);
    return 0;
  }

  fprintf(stderr, "✓ SUCCESS!\n");
  fprintf(stderr, "Result mug: %x\n", u3r_mug(u3t(pro)));
  fprintf(stderr, "\nResult structure:\n");
  u3m_p("result", u3t(pro));
  fprintf(stderr, "\n");

  fprintf(stderr, "\nThis is UNEXPECTED! The formula somehow works on atom 0 in C!\n");

  u3z(pro);

  return 1;
}

int
main(int argc, char* argv[])
{
  _setup();

  if ( !_test_life_on_null() ) {
    fprintf(stderr, "test life on null: failed (as expected)\r\n");
    fprintf(stderr, "This is actually the CORRECT behavior - lifecycle formula cannot work on null.\r\n");
    return 0;  // Return success since failure is expected
  }

  //  GC
  //
  u3m_grab(u3_none);

  fprintf(stderr, "test life on null: unexpected success!\r\n");
  return 0;
}