Author: fperrad Date: Fri Dec 26 03:51:07 2008 New Revision: 34375 Added: trunk/languages/lua/t/bit/ trunk/languages/lua/t/bit/bittest.lua (contents, props changed) trunk/languages/lua/t/bit/nsievebits.lua (contents, props changed) Modified: trunk/MANIFEST trunk/MANIFEST.SKIP trunk/languages/lua/src/lib/bit.pir trunk/languages/lua/t/bit.t
Log: [Lua] bit - some fix - add official test suite Modified: trunk/MANIFEST ============================================================================== --- trunk/MANIFEST (original) +++ trunk/MANIFEST Fri Dec 26 03:51:07 2008 @@ -1,7 +1,7 @@ # ex: set ro: # $Id$ # -# generated by tools/dev/mk_manifest_and_skip.pl Wed Dec 24 13:06:20 2008 UT +# generated by tools/dev/mk_manifest_and_skip.pl Fri Dec 26 11:40:35 2008 UT # # See tools/dev/install_files.pl for documentation on the # format of this file. @@ -1532,6 +1532,9 @@ languages/ecmascript/MAINTAINER [ecmascript] languages/ecmascript/config/makefiles/root.in [ecmascript] languages/ecmascript/js.pir [ecmascript] +languages/ecmascript/lib/Parrot/Test/JS.pm [ecmascript] +languages/ecmascript/lib/Parrot/Test/JS/PJS.pm [ecmascript] +languages/ecmascript/lib/Parrot/Test/JS/SpiderMonkey.pm [ecmascript] languages/ecmascript/src/builtin/builtins.pir [ecmascript] languages/ecmascript/src/classes/Boolean.pir [ecmascript] languages/ecmascript/src/classes/Completion.pir [ecmascript] @@ -1548,8 +1551,12 @@ languages/ecmascript/t/01-literals.t [ecmascript] languages/ecmascript/t/02-operators.t [ecmascript] languages/ecmascript/t/02-sanity-var.t [ecmascript] -#languages/ecmascript/t/03-boolean.t [ecmascript] languages/ecmascript/t/harness [ecmascript] +languages/ecmascript/t/js_pt/10-version.t [ecmascript] +languages/ecmascript/t/sanity_pt/00-literals.t [ecmascript] +languages/ecmascript/t/sanity_pt/01-vars.t [ecmascript] +languages/ecmascript/t/sanity_pt/03-boolean.t [ecmascript] +languages/ecmascript/t/sanity_pt/05-objects.t [ecmascript] languages/forth/MAINTAINER [forth] languages/forth/config/makefiles/root.in [forth] languages/forth/forth.pir [forth] @@ -1828,6 +1835,8 @@ languages/lua/t/basic.t [lua] languages/lua/t/bc.t [lua] languages/lua/t/bit.t [lua] +languages/lua/t/bit/bittest.lua [lua] +languages/lua/t/bit/nsievebits.lua [lua] languages/lua/t/bitlib.t [lua] languages/lua/t/boolean.t [lua] languages/lua/t/closure.t [lua] Modified: trunk/MANIFEST.SKIP ============================================================================== --- trunk/MANIFEST.SKIP (original) +++ trunk/MANIFEST.SKIP Fri Dec 26 03:51:07 2008 @@ -1,6 +1,6 @@ # ex: set ro: # $Id$ -# generated by tools/dev/mk_manifest_and_skip.pl Wed Dec 24 13:06:20 2008 UT +# generated by tools/dev/mk_manifest_and_skip.pl Fri Dec 26 11:40:35 2008 UT # # This file should contain a transcript of the svn:ignore properties # of the directories in the Parrot subversion repository. (Needed for @@ -752,6 +752,16 @@ # generated from svn:ignore of 'languages/ecmascript/src/' ^languages/ecmascript/src/gen_.*\.pir$ ^languages/ecmascript/src/gen_.*\.pir/ +# generated from svn:ignore of 'languages/ecmascript/t/js_pt/' +^languages/ecmascript/t/js_pt/.*\.js$ +^languages/ecmascript/t/js_pt/.*\.js/ +^languages/ecmascript/t/js_pt/.*\.out$ +^languages/ecmascript/t/js_pt/.*\.out/ +# generated from svn:ignore of 'languages/ecmascript/t/sanity_pt/' +^languages/ecmascript/t/sanity_pt/.*\.js$ +^languages/ecmascript/t/sanity_pt/.*\.js/ +^languages/ecmascript/t/sanity_pt/.*\.out$ +^languages/ecmascript/t/sanity_pt/.*\.out/ # generated from svn:ignore of 'languages/forth/' ^languages/forth/Makefile$ ^languages/forth/Makefile/ Modified: trunk/languages/lua/src/lib/bit.pir ============================================================================== --- trunk/languages/lua/src/lib/bit.pir (original) +++ trunk/languages/lua/src/lib/bit.pir Fri Dec 26 03:51:07 2008 @@ -211,7 +211,6 @@ $I1 = lua_checknumber(1, x) $I2 = lua_checknumber(2, n) band $I2, 31 - neg $I2 rot $I0, $I1, $I2, 32 .BRET($I0) .end @@ -223,6 +222,7 @@ $I1 = lua_checknumber(1, x) $I2 = lua_checknumber(2, n) band $I2, 31 + neg $I2 rot $I0, $I1, $I2, 32 .BRET($I0) .end @@ -239,8 +239,8 @@ .param pmc x :optional .param pmc extra :slurpy $I1 = lua_checknumber(1, x) - $I0 = $I1 >> 24 - $I2 = $I1 >> 8 + $I0 = $I1 >>> 24 + $I2 = $I1 >>> 8 $I2 &= 0x0000ff00 $I0 |= $I2 $I3 = $I1 & 0x0000ff00 Modified: trunk/languages/lua/t/bit.t ============================================================================== --- trunk/languages/lua/t/bit.t (original) +++ trunk/languages/lua/t/bit.t Fri Dec 26 03:51:07 2008 @@ -32,7 +32,7 @@ plan skip_all => "parrot only"; } else { - plan tests => 6; + plan tests => 8; } language_output_is( 'lua', << 'CODE', << "OUTPUT", 'require' ); @@ -69,9 +69,24 @@ language_output_is( 'lua', << 'CODE', << "OUTPUT", 'bit.bswap' ); m = require "bit" assert(bit.bswap(0x12345678) == 0x78563412) +assert(bit.bswap(0x9ABCDEF0) == 0xF0DEBC9A) CODE OUTPUT +my $code; + +$code = Parrot::Test::slurp_file( "$FindBin::Bin/bit/bittest.lua" ); +$code .= "\nprint 'ok'\n"; +language_output_is( 'lua', $code, << "OUTPUT", 'bittest' ); +ok +OUTPUT + +$code = Parrot::Test::slurp_file( "$FindBin::Bin/bit/nsievebits.lua" ); +$code .= "\nprint 'ok'\n"; +language_output_is( 'lua', $code, << "OUTPUT", 'nsievebits' ); +ok +OUTPUT + # Local Variables: # mode: cperl Added: trunk/languages/lua/t/bit/bittest.lua ============================================================================== --- (empty file) +++ trunk/languages/lua/t/bit/bittest.lua Fri Dec 26 03:51:07 2008 @@ -0,0 +1,91 @@ +-- Test cases for bit operations library. Public domain. + +local bit = require"bit" + +local vb = { + 0, 1, -1, 2, -2, 0x12345678, 0x87654321, + 0x33333333, 0x77777777, 0x55aa55aa, 0xaa55aa55, + 0x7fffffff, 0x80000000, 0xffffffff +} + +local function cksum(name, s, r) + local z = 0 + for i=1,#s do z = (z + string.byte(s, i)*i) % 2147483629 end + if z ~= r then + error("bit."..name.." test failed (got "..z..", expected "..r..")", 0) + end +end + +local function check_unop(name, r) + local f = bit[name] + local s = 0; + if pcall(f) or pcall(f, "z") or pcall(f, true) then + error("bit."..name.." fails to detect argument errors", 0) + end + for _,x in ipairs(vb) do s = s..","..tostring(f(x)) end + cksum(name, s, r) +end + +local function check_binop(name, r) + local f = bit[name] + local s = 0; + if pcall(f) or pcall(f, "z") or pcall(f, true) then + error("bit."..name.." fails to detect argument errors", 0) + end + for _,x in ipairs(vb) do + for _,y in ipairs(vb) do s = s..","..tostring(f(x, y)) end + end + cksum(name, s, r) +end + +local function check_shift(name, r) + local f = bit[name] + local s = 0; + if pcall(f) or pcall(f, "z") or pcall(f, true) or pcall(f, 1) then + error("bit."..name.." fails to detect argument errors", 0) + end + for _,x in ipairs(vb) do + for y=0,31 do s = s..","..tostring(f(x, y)) end + end + cksum(name, s, r) +end + +-- Minimal sanity checks. +assert(0x7fffffff == 2147483647, "broken hex literals") +assert(0xffffffff == -1 or 0xffffffff == 2^32-1, "broken hex literals") +assert(tostring(-1) == "-1", "broken tostring()") +assert(tostring(0xffffffff) == "-1" or tostring(0xffffffff) == "4294967295", "broken tostring()") + +-- Basic argument processing. +assert(bit.tobit(1) == 1) +assert(bit.band(1) == 1) +assert(bit.bxor(1,2) == 3) +assert(bit.bor(1,2,4,8,16,32,64,128) == 255) + +-- Apply operations to test vectors and compare checksums. +check_unop("tobit", 282601) +check_unop("bnot", 293242) +--check_unop("bswap", 313216) + +check_binop("band", 41271123) +check_binop("bor", 51325358) +check_binop("bxor", 79412059) + +check_shift("lshift", 325441968) +check_shift("rshift", 139179769) +check_shift("arshift", 111469290) +check_shift("rol", 302576086) +check_shift("ror", 302491692) + +-- Do this last, since quite a few Lua installations fail. +-- Note: this is not BitOp's fault, check the casts for case 'x' in +-- str_format() in src/lstrlib.c. +local s = string.format("%08x", 1) +assert(s == "00000001", "broken string.format(\"%08x\", 1)") +s = string.format("%x", 0x7fffffff) +assert(s == "7fffffff", "broken string.format(\"%x\", 0x7fffffff)") +s = string.format("%x", -1) +assert(s == "ffffffff" or s == "ffffffffffffffff", "broken string.format(\"%x\", -1)") +s = string.format("%x", 0xffffffff) +assert(s == "ffffffff" or s == "ffffffffffffffff", "broken string.format(\"%x\", 0xffffffff)") + Added: trunk/languages/lua/t/bit/nsievebits.lua ============================================================================== --- (empty file) +++ trunk/languages/lua/t/bit/nsievebits.lua Fri Dec 26 03:51:07 2008 @@ -0,0 +1,32 @@ +-- This is the (naive) Sieve of Eratosthenes. Public domain. + +local bit = require("bit") +local band, bxor, rshift, rol = bit.band, bit.bxor, bit.rshift, bit.rol + +local function nsieve(p, m) + local count = 0 + for i=0,(m+31)/32 do p[i] = -1 end + for i=2,m do + if band(rshift(p[rshift(i, 5)], i), 1) ~= 0 then + count = count + 1 + for j=i+i,m,i do + local jx = rshift(j, 5) + p[jx] = band(p[jx], rol(-2, j)) + end + end + end + return count +end + +if arg and arg[1] then + local N = tonumber(arg[1]) or 1 + if N < 2 then N = 2 end + local primes = {} + + for i=0,2 do + local m = (2^(N-i))*10000 + io.write(string.format("Primes up to %8d %8d\n", m, nsieve(primes, m))) + end +else + assert(nsieve({}, 10000) == 1229) +end