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

     1  a = 2
     2  b = (a + 1) / (a - 1)
     3  assert( b == 3)
     4  assert( "sep0")
     5  
     6  assert( (a + 1) / (a - 1) == 3)
     7  assert( "sep1")
     8  
     9  assert( 3 == (a + 1) / (a - 1))
    10  assert( "sep2")
    11  
    12  assert( (a + 1) == (a + 1) / (a - 1))
    13  assert( "sep3")
    14  
    15  function sub1(a) return a - 1 end
    16  assert( (a + 1) == (a + 1) / sub1(a))
    17  assert( "sep4")
    18  
    19  assert( a + (a + (a + (a + 1))) == 9)
    20  assert( "sep5")
    21  
    22  assert( 1 + (1 + (1 + (1 + a))) == 6)
    23  assert( "sep6")
    24  
    25  c = [ 1,2,3,4 ]
    26  assert( 1 + (1 + (1 + (1 + a))) + #(c) , 10)
    27  assert( "sep7")
    28  
    29   a = 10
    30  assert( 1 + (1 + a) == 12)
    31  
    32  function foo1(a, b, c)
    33  return a, b, c
    34  end
    35  
    36  local a, b, c = foo1(1, 2, 3)
    37  assert(a == 1 and b + c == 5)
    38  
    39  function foo2(a, b, c, x)
    40      local tmp = [a, b, c]
    41  	if x then
    42  		for i = 0, #(x) do tmp.append(x[i]) end
    43  	end
    44      return tmp
    45  end
    46  
    47  local _, _, _, x,y = foo2(1,2,3,["4",5])
    48  assert(x == "4" and y == 5)
    49  
    50  function remove(el, arr)
    51      for i=0, #(arr) do
    52  	    if arr[i] == el then
    53              for i=i,#(arr)-1 do
    54                  arr[i] = arr[i+1]
    55              end
    56              arr[#(arr) - 1] = nil
    57  	        return arr
    58  	    end
    59      end
    60      return arr
    61  end
    62  
    63  do
    64  	local r = remove(1, [1, 2, 3])
    65  	assert(r[0] == 2 and r[1] == 3)
    66  	local r = remove(2, [1, 2, 3])
    67  	assert(r[0] == 1 and r[1] == 3)
    68  	local r = remove(3, [1, 2, 3])
    69  	println(r)
    70  	assert(r[0] == 1 and r[1] == 2)
    71  end
    72  
    73  function bad(n) panic("bad" + n) end
    74  do
    75  	local err = bad.try("boy")
    76  	assert(err.error() == "badboy")
    77  end
    78  
    79  function intersect(a, b)
    80  	a.foreach(function(k, v)
    81  		if not self.b.contains(k) then return false end
    82  	end)
    83  	return a
    84  end
    85  
    86  a = intersect({a=1, b=1, c=3}, {a=2, c:4})
    87  assert(a.a, 1)
    88  assert(a.c, 3)
    89  
    90  assert(structAddrTest.Foo(), structAddrTest.A)
    91  assert(structAddrTest2.Foo(), structAddrTest2.A)
    92  
    93  
    94  function foo(a, b, c) return if(a,b,c) end
    95  
    96  assert(foo(true, 10, 20), 10)
    97  
    98  print('---R2---')
    99  
   100  assert(1 is not object)
   101  assert([1,2] is array)
   102  assert([1,2] is native)
   103  assert([1,2] is not nativemap)
   104  assert([1,2] is not object)
   105  assert(native is not object)
   106  
   107  
   108  function foo3(k) debug.self()[k] = k end
   109  for i=0,10 do foo3(i) end
   110  for i=0,10 do assert(foo3[i], i) end
   111  
   112  oneline = {}
   113  
   114  function oneline.write(b)
   115      b = b.unsafestr()
   116      x = #b
   117      b = b.replace('\n', '').replace('\t', '')
   118      b = re([[\s+]]).replace(b, '')
   119      this.buf += b
   120      return x
   121  end
   122  
   123  ol = new(oneline, {buf=''})
   124  os.shell("ls -al", {stdout=ol})
   125  print(ol.buf)
   126  
   127  ol = buffer()
   128  os.shell("whoami", {stdout=ol})
   129  print(ol.value().trim())
   130  
   131  local bigint = createprototype("bigint", function(s)
   132      this.buf = bytes(0)
   133      for i=#s-1,-1,-1 do
   134          this.buf.append(s[i] - 48)
   135      end
   136  end, {})
   137  
   138  function bigint.tostring()
   139      local tmp = ''
   140      for _, b in this.buf do
   141          tmp = chr(48 + b) + tmp
   142      end
   143      return tmp
   144  end
   145  
   146  function bigint.equals(rhs)
   147      if #this.buf != #rhs.buf then return false end
   148      for i=0,#this.buf do
   149          if this.buf[i] != rhs.buf[i] then return false end
   150      end
   151      return true
   152  end
   153  
   154  function bigint.add(rhs)
   155      for i=#this.buf,#rhs.buf do this.buf.append(0) end
   156      local carry = 0
   157      for i=0,#this.buf do
   158          if i < #rhs.buf then
   159              this.buf[i] += rhs.buf[i] + carry
   160          else
   161              if carry == 0 then break end
   162              this.buf[i] += carry
   163          end
   164  
   165          if this.buf[i] < 10 then 
   166              carry = 0
   167              continue
   168          end
   169          carry = 1
   170          this.buf[i] -= 10
   171      end
   172      if carry != 0 then
   173          this.buf.append(1)
   174      end
   175      return this
   176  end
   177  
   178  assert(bigint("1234").tostring(), "1234")
   179  assert(bigint("9").add(bigint("2")).equals(bigint("11")))
   180  assert(bigint("9").add(bigint("99")).equals(bigint("108")))
   181  assert(bigint("9999999999999999999999999999").add(bigint("2")).
   182  equals(bigint("10000000000000000000000000001")))
   183  
   184  local f = eval("a=1 return function(n) a +=n return a end")
   185  assert(f(1), 2)
   186  assert(f(2), 4)
   187  
   188  function clsrec(a)
   189      a += 1
   190      function N()
   191          if self.a == 1 then return 1 end 
   192          self.a -= 1
   193          return self.a * self.N()
   194      end
   195      return N
   196  end
   197  assert(clsrec(5)(), 120)
   198  
   199  function clsarray(a, n)
   200      local res = []
   201      for i=0,n do
   202          res.append(function()
   203              do
   204                  return self.a * (self.i + 1)
   205              end
   206          end)
   207      end
   208      return res
   209  end
   210  assert(#clsarray.caplist(), 0)
   211  
   212  for i, f in clsarray(10, 10) do
   213      assert(f(), (i + 1) * 10)
   214  end
   215  
   216  function createlinklist(a...)
   217      if #a == 0 then return end
   218      local head = [a[0], nil]
   219      local tmp = head
   220      for i=1,#a do
   221          tmp[1] = [a[i], nil]
   222          tmp = tmp[1]
   223      end
   224      return head
   225  end
   226  
   227  function reverselinklist(lst)
   228      if not lst then return lst end
   229      local dummy = ['lead', nil]
   230      while lst do
   231          local old = dummy[1]
   232          local x = [lst[0], old]
   233          dummy[1] = x
   234          lst = lst[1]
   235      end
   236      return dummy[1]
   237  end
   238  
   239  ll = createlinklist(1, 2, 3, 4)
   240  print(ll, reverselinklist(ll))
   241  
   242  function andortest(a, b, c)
   243      if c then return a[0] or b[1] end
   244      return a[0] and b[1]
   245  end
   246  
   247  assert(andortest([0], [0, 1], true), 1)
   248  assert(andortest([0], [0, 1], false), 0)
   249  assert(andortest([2], [0, "1"], false), "1")
   250  
   251  function firstK(a, b, k)
   252      a.sort()
   253      b.sort()
   254  
   255      while k do
   256          if #a == 0 and #b == 0 then break end
   257          if #a == 0 then
   258              b = b[1:]
   259          elseif #b == 0 then
   260              a = a[1:]
   261          elseif a[0] < b[0] then
   262              a = a[1:]
   263          else
   264              b = b[1:]
   265          end
   266          k -= 1
   267      end
   268  
   269      if k >= #a and k >= #b then return "not found" end
   270      if #a and #b then return math.min(a[0], b[0]) end
   271      if #a then return a[0] end
   272      if #b then return b[0] end
   273      panic("shouldn't happen")
   274  end
   275  
   276  assert(firstK([1,2,3], [4,5,6], 1), 2)
   277  assert(firstK([1,2,3], [0.4,1.5,2.6], 2), 1.5)
   278  assert(firstK([1,2,3], [0.7], 2), 2)
   279  
   280  local tmp = []
   281  for i=0,1e3 do tmp += math.random() end
   282  ki = int(math.random() * #tmp)
   283  k = tmp.clone().sort()[ki]
   284  a, b = tmp[:#tmp / 2], tmp[#tmp/ 2:]
   285  assert(firstK(a, b, ki), k)
   286  
   287  function fibwrapper() end
   288  function fibwrapper.next(ab)
   289      if ab[0] == nil then
   290          this.v = 1
   291          return 1, this.v
   292      end
   293      this.v = ab[0] + ab[1]
   294      ab[0], ab[1] = ab[1], this.v
   295      return ab
   296  end
   297  
   298  local nw = createnativewrapper(fibwrapper)
   299  res = []
   300  for k, v in nw({}) do 
   301      res.append(v)
   302      if #res > 35 then break end
   303  end
   304  assert(res[35], 24157817)
   305  
   306  function runtimejump(v)
   307      if v == 1 then return jump("label1") end
   308      if v == 2 then return jump("label2") end
   309      return jump("labeldefault")
   310  end
   311  
   312  runtimejump(3)
   313  ::label1:: assert(false)
   314  ::label2:: assert(false)
   315  ::labeldefault:: assert(true) print("runtimejump 3")
   316  
   317  function test_jump()
   318      local ch1 = channel()
   319      local ch2 = channel()
   320  
   321      function() time.sleep(0.2) self.ch1.send("ch1") end.go()
   322  
   323      channel.recvmulti2(local out[2], {ch1: goto ch1, ch2: goto ch2})
   324      ::ch2::
   325      assert(false)
   326      if::ch1::then
   327      assert(out[0], "ch1")
   328      print(out)
   329      end
   330  end
   331  test_jump()