testsuite.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. --[[
  2. This software is licensed under the zlib license.
  3. (C) Copyright 2015 Pedro Gimeno Fortea. All rights reserved.
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the author or authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. ]]
  18. strbit = require "strbit"
  19. local good
  20. local bad
  21. local seed
  22. local function to_bits(s)
  23. local out = ""
  24. for i = 1, #s do
  25. local byte = s:byte(i)
  26. local this = ""
  27. for bit = 0, 7 do
  28. this = (byte % 2) .. this
  29. byte = (byte - byte % 2) / 2
  30. end
  31. out = out .. this
  32. end
  33. return out
  34. end
  35. local function bshiftleft(s, i)
  36. len = #s
  37. if i < 0 then
  38. i = len+i
  39. if i < 0 then i = 0 end
  40. s = string.rep("0", len) .. s
  41. return s:sub(i+1, i+len)
  42. end
  43. s = s .. string.rep("0", len)
  44. if i > len then i = len end
  45. return s:sub(i+1, i+len)
  46. end
  47. local function band(s1, s2)
  48. local s = ""
  49. if #s1 > #s2 then
  50. s1 = s1:sub(1, #s2)
  51. end
  52. for i = 1, #s1 do
  53. s = s .. ((s1:byte(i)-48) * (s2:byte(i)-48))
  54. end
  55. return s
  56. end
  57. local function bor(s1, s2)
  58. local s = ""
  59. if #s1 > #s2 then
  60. s1 = s1:sub(1, #s2)
  61. end
  62. for i = 1, #s1 do
  63. s = s .. (s1:byte(i)-48) + (s2:byte(i)-48) - (s1:byte(i)-48)*(s2:byte(i)-48)
  64. end
  65. return s
  66. end
  67. local function bxor(s1, s2)
  68. local s = ""
  69. if #s1 > #s2 then
  70. s1 = s1:sub(1, #s2)
  71. end
  72. for i = 1, #s1 do
  73. s = s .. (s1:byte(i) + s2:byte(i)) % 2
  74. end
  75. return s
  76. end
  77. local function bnot(s1)
  78. local s = ""
  79. for i = 1, #s1 do
  80. s = s .. string.char(97-s1:byte(i))
  81. end
  82. return s
  83. end
  84. local function test_shift(s, i)
  85. local with_lua = bshiftleft(to_bits(s), i)
  86. local with_strbit = to_bits(strbit.bshiftleft(s, i))
  87. if with_lua == with_strbit then
  88. good = good + 1
  89. else
  90. bad = bad + 1
  91. print("Difference: shift of "..i)
  92. print("Original = "..to_bits(s))
  93. print("with_lua = "..with_lua)
  94. print("with_strbit = "..with_strbit)
  95. end
  96. end
  97. local function test_binop(s1, s2, luafn, libfn, name)
  98. local with_lua = luafn(to_bits(s1), to_bits(s2))
  99. local with_strbit = to_bits(libfn(s1, s2))
  100. if with_lua == with_strbit then
  101. good = good + 1
  102. else
  103. bad = bad + 1
  104. print("Difference in "..name)
  105. print("Original 1 = "..to_bits(s1))
  106. print("Original 2 = "..to_bits(s2))
  107. print("with_lua = "..with_lua)
  108. print("with_strbit = "..with_strbit)
  109. end
  110. end
  111. local function test_not(s)
  112. local with_lua = bnot(to_bits(s))
  113. local with_strbit = to_bits(strbit.bnot(s))
  114. if with_lua == with_strbit then
  115. good = good + 1
  116. else
  117. bad = bad + 1
  118. print("Difference: not ")
  119. print("Original = "..to_bits(s))
  120. print("with_lua = "..with_lua)
  121. print("with_strbit = "..with_strbit)
  122. end
  123. end
  124. good = 0
  125. bad = 0
  126. test_shift("", -30)
  127. test_shift("", -8)
  128. test_shift("", -3)
  129. test_shift("", 0)
  130. test_shift("", 3)
  131. test_shift("", 8)
  132. test_shift("", 30)
  133. seed = os.time()
  134. print("Seed = " .. seed)
  135. math.randomseed(seed)
  136. local olds
  137. local s = ""
  138. for test = 1, 10000 do
  139. len = math.random(0,8)
  140. olds = s
  141. s = ""
  142. for i = 1, len do
  143. s = s .. string.char(math.random(0, 255))
  144. end
  145. test_shift(s, math.random(-80, 80))
  146. test_binop(olds, s, band, strbit.band, "AND")
  147. test_binop(olds, s, bor, strbit.bor, "OR")
  148. test_binop(olds, s, bxor, strbit.bxor, "XOR")
  149. test_not(s)
  150. end
  151. print("Good: " .. good .. "; bad: " .. bad)