github.com/coyove/nj@v0.0.0-20221110084952-c7f8db1065c3/tests/struct.nj.lua (about)

     1  function makecls(f, c)
     2      a = function(x...)
     3          o = debug.self()
     4          return o._f(o._c, x...)
     5      end.copy()
     6      a.setproto({_f=f, _c=c}['setproto'](func))
     7      return a
     8  end
     9  
    10  a = makecls(function(x, step) x.c=x.c+step return x.c end, {c=0})
    11  b = makecls(function(x, step) x.c=x.c+step return x.c end, {c=0})
    12  assert(a(1), 1)
    13  assert(b(2), 2)
    14  assert(a(1), 2)
    15  assert(b(2), 4)
    16  assert(a is callable)
    17  
    18  timer = {}
    19  function timer.reset()
    20      this.c = 0
    21  end
    22  function timer.add(v)
    23      this.c = this.c + v + this.X
    24  end
    25  T1, T2 = new(timer, {c=0,X=10}), new(timer, {c=0,X=10})
    26  T1.add(2)
    27  T2.add(3)
    28  assert(T1.c == 12 and T2.c == 13)
    29  
    30  println(T1.c, T2.c)
    31  T1.X = 100
    32  println(T1.c, T2.c)
    33  T1.add(20)
    34  println(T1.c, T2.c)
    35  T2.add(30)
    36  assert(T1.c == 132 and T2.c == 53)
    37  
    38  print(timer.add)
    39  
    40  assert(structAddrTestEmbed.T.SetFoo, nil)
    41  structAddrTestEmbed.natptrat('T').SetFoo(10)
    42  assert(structAddrTestEmbed.T.A, 10)
    43  
    44  function dict() return new(new(self)) end
    45  
    46  function dict.next2()
    47      local k, v = this.next(this.proto()._iter)
    48      this.proto()._iter = k
    49      return k, v
    50  end
    51  
    52  m = dict()
    53  m[0] = 0
    54  m[true] = false
    55  m.zz = 'zz'
    56  while true do
    57      k, v = m.next2()
    58      print(k ,v)
    59      if k == nil then
    60          break
    61      end
    62      assert(m[k] == v)
    63      println(k ,v)
    64  end
    65  
    66  CarPrototype = {}
    67  function CarPrototype.getBrand() return this.brand end
    68  function CarPrototype.setBrand(brand) this.brand = brand; return this end
    69  function CarPrototype.toString() return ('%s %s').format(this.color, this.brand) end
    70  
    71  Car = new(CarPrototype, {brand=''})
    72  
    73  carA = new(Car.copy(), {color='red'}).setBrand('ferrari')
    74  carB = new(Car.copy(), {color='orange'}).setBrand('mclaren')
    75  
    76  assert(carA.toString(), "red ferrari")
    77  assert(carB.toString(), "orange mclaren")
    78  
    79  cnt = 0
    80  k = 99
    81  for k, v in m do
    82      cnt = cnt + 1
    83      assert(m[k] == v)
    84  end
    85  assert(cnt == 3)
    86  assert(k == 99) -- k shouldn't be altered
    87  
    88  function worker()
    89      time.sleep(1)
    90      print("worker after 1s")
    91      return 'finished'
    92  end
    93  assert(worker.map([0], os.numcpus)[0] == 'finished')
    94  
    95  -- closure
    96  function foo(a)
    97      return function(b)
    98          return function(c)
    99              self.a = self.a + 1
   100              if c == 6 then panic(self.a + self.b + c) end
   101              return self.a + self.b + c
   102          end
   103      end
   104  end
   105  bar = foo(2)(3)
   106  assert(bar(4), 10)
   107  assert(bar(5), 12)
   108  
   109  bar = foo(2)(3)
   110  assert(bar(4), 10)
   111  assert(bar(5), 12)
   112  assert(bar.try(6).error(), 14)
   113  local x, _, _ = bar.try(6)
   114  assert(x.error(), 15)
   115  
   116  function addcls(a)
   117      local a = 3
   118      return function(b)
   119          local b = 2
   120          return function(c)
   121              return self.a + self.b + c
   122          end
   123      end
   124  end
   125  assert(addcls(1)(2)(3), 8)
   126  
   127  print.try("native try call")
   128  assert(function(x) assert(x, 1) return x end.try(1) is (1))
   129  assert(function(x) assert(x, 1) return x end.try(2) is error)
   130  
   131  -- syntax test
   132  m = {}
   133  function m.a() return 'a' end
   134  assert(m.a() == 'a')
   135  
   136  cls = {}
   137  function cls.__str() return this.v end
   138  cls2 = {}
   139  function cls2.__str() return this.v + '2' end
   140  cls2 = new(cls, cls2)
   141  obj = new(cls, {v='obj'})
   142  obj2 = new(cls2, {v='obj'})
   143  assert((obj).__str(), 'obj')
   144  assert((obj2).__str(), 'obj2')
   145  assert(obj2 is cls)
   146  print(obj2)
   147  
   148  t = {a=1, b=2}
   149  t2 = t.copy(true)
   150  t.b =3
   151  assert(t2.b, 2)
   152  
   153  t = ([1,2,3,4]).filter(function(v) return v >= 2 end)
   154  assert(t[0] == 2 and t[2] == 4)
   155  
   156  
   157  f1 = open("tests/a.txt", "w+")
   158  f1.write("hello zzz")
   159  f1.close()
   160  
   161  f1 = open("tests/a.txt", "r")
   162  f2 = open("tests/b.txt", "w+")
   163  f2.write(f1.read(5))
   164  f1.close()
   165  f2.write(" world")
   166  f2.close()
   167  
   168  assert(open("tests/b.txt").read().unsafestr(), "hello world")
   169  open.close()
   170  
   171  
   172  f1 = open("tests/a.txt", "r")
   173  f2 = open("tests/b.txt", "a+")
   174  f2.pipe(f1)
   175  f1.close()
   176  f2.close()
   177  
   178  assert(open("tests/b.txt").read().unsafestr(), "hello worldhello zzz")
   179  open.close()
   180  
   181  os.remove("tests/a.txt")
   182  os.remove("tests/b.txt")
   183  
   184  
   185  m = gomap({a=1,b=2}, 'c', 3)
   186  print(type(m))
   187  assert(m is nativemap)
   188  assert(m.c , 3)
   189  assert(#m, 3)
   190  m.merge({d=4, e=5})
   191  assert(m.contains('e'))
   192  print(m.keys())
   193  
   194  
   195  ch = channel(1)
   196  ch.send(1)
   197  assert(ch.recv()[0], 1)
   198  
   199  ch2 = channel(1)
   200  flag = false
   201  function()
   202      ch.send(1)
   203      time.sleep(1)
   204      ch2.send(2)
   205      flag = true
   206  end.go()
   207  
   208  local rch, rv = channel.recvmulti([ch, ch2])
   209  assert(rch, ch)
   210  assert(rv, 1)
   211  local rch, rv = channel.recvmulti([ch, ch2])
   212  assert(rch, ch2)
   213  assert(rv, 2)
   214  assert(flag)
   215  
   216  loadfile("tests/array.nj.lua")
   217  
   218  assert(goVarg(10, function(a, b...)
   219      for _, b in b do
   220          a *= b
   221      end
   222      println(a, b)
   223      return a 
   224  end), 10 * 11 * 12)
   225