Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 8d8483abc3
Fetching contributors…

Cannot retrieve contributors at this time

executable file 222 lines (188 sloc) 7.777 kb
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
#!/usr/bin/ruby -KU
# Copyright 2011 Zuse Institute Berlin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

begin
  require "#{File.dirname(__FILE__)}/scalaris"
rescue LoadError
  require "scalaris"
end

def read_or_write(sc, key, value, mode, binary = false)
  begin
    if (mode == :read)
      puts 'read(' + key.to_s + ')'
      puts ' expected: ' + value.inspect
      actual = sc.read(key)
      puts ' read raw: ' + actual.inspect
      # if the expected value is a list, the returned value could by (mistakenly) a string if it is a list of integers
      # -> convert such a string to a list
      if value.is_a?(Array)
        begin
            actual = Scalaris::str_to_list(actual)
        rescue Exception => e
            puts 'fail'
            return 1
        end
      end
      puts ' read rb: ' + actual.inspect
      if (actual == value)
        puts 'ok'
        return 0
      else
        puts 'fail'
        return 1
      end
    elsif (mode == :write)
      puts 'write(' + key.to_s + ', ' + value.to_s + ')'
      sc.write(key, value, binary)
      return 0
    end
  rescue Scalaris::ConnectionError => instance
    puts 'failed with connection error'
    return 1
  rescue Scalaris::TimeoutError => instance
    puts 'failed with timeout'
    return 1
  rescue Scalaris::AbortError => instance
    puts 'failed with abort'
    return 1
  rescue Scalaris::NotFoundError => instance
    puts 'failed with not_found'
    return 1
  rescue Scalaris::UnknownError => instance
    puts 'failed with unknown: ' + instance.inspect
    return 1
  rescue Exception => instance
    puts 'failed with ' + instance.inspect
    return 1
  end
end

def read_write_boolean(basekey, sc, mode)
  failed = 0
  
  failed += read_or_write(sc, basekey + "_bool_false", false, mode)
  failed += read_or_write(sc, basekey + "_bool_true", true, mode)
  
  return failed
end

def read_write_integer(basekey, sc, mode)
  failed = 0
  
  failed += read_or_write(sc, basekey + "_int_0", 0, mode)
  failed += read_or_write(sc, basekey + "_int_1", 1, mode)
  failed += read_or_write(sc, basekey + "_int_min", -2147483648, mode)
  failed += read_or_write(sc, basekey + "_int_max", 2147483647, mode)
  failed += read_or_write(sc, basekey + "_int_max_div_2", 2147483647 / 2, mode)
  
  return failed
end

def read_write_long(basekey, sc, mode)
  failed = 0
  
  failed += read_or_write(sc, basekey + "_long_0", 0, mode)
  failed += read_or_write(sc, basekey + "_long_1", 1, mode)
  failed += read_or_write(sc, basekey + "_long_min", -9223372036854775808, mode)
  failed += read_or_write(sc, basekey + "_long_max", 9223372036854775807, mode)
  failed += read_or_write(sc, basekey + "_long_max_div_2", 9223372036854775807 / 2, mode)
  
  return failed
end
  
def read_write_biginteger(basekey, sc, mode)
  failed = 0
  
  failed += read_or_write(sc, basekey + "_bigint_0", 0, mode)
  failed += read_or_write(sc, basekey + "_bigint_1", 1, mode)
  failed += read_or_write(sc, basekey + "_bigint_min", -100000000000000000000, mode)
  failed += read_or_write(sc, basekey + "_bigint_max", 100000000000000000000, mode)
  failed += read_or_write(sc, basekey + "_bigint_max_div_2", 100000000000000000000 / 2, mode)
  
  return failed
end

def read_write_double(basekey, sc, mode)
  failed = 0
  
  failed += read_or_write(sc, basekey + "_float_0.0", 0.0, mode)
  failed += read_or_write(sc, basekey + "_float_1.5", 1.5, mode)
  failed += read_or_write(sc, basekey + "_float_-1.5", -1.5, mode)
  failed += read_or_write(sc, basekey + "_float_min", 4.9E-324, mode)
  failed += read_or_write(sc, basekey + "_float_max", 1.7976931348623157E308, mode)
  failed += read_or_write(sc, basekey + "_float_max_div_2", 1.7976931348623157E308 / 2.0, mode)
  
  # not supported by erlang:
  #failed += read_or_write(sc, basekey + "_float_neg_inf", float('-inf'), mode)
  #failed += read_or_write(sc, basekey + "__float_pos_inf", float('+inf'), mode)
  #failed += read_or_write(sc, basekey + "_float_nan", float('nan'), mode)
  
  return failed
end

def read_write_string(basekey, sc, mode)
  failed = 0
  
  failed += read_or_write(sc, basekey + "_string_empty", '', mode)
  failed += read_or_write(sc, basekey + "_string_foobar", 'foobar', mode)
  failed += read_or_write(sc, basekey + "_string_foo\\nbar", "foo\nbar", mode)
  failed += read_or_write(sc, basekey + "_string_unicode", 'foo' + [0x0180, 0x01E3, 0x11E5].pack("U*"), mode)
  
  return failed
end

def read_write_binary(basekey, sc, mode)
  failed = 0
  
  # note: binary not supported by JSON
  failed += read_or_write(sc, basekey + "_byte_empty", [].pack("c*"), mode, true)
  failed += read_or_write(sc, basekey + "_byte_0", [0].pack("c*"), mode, true)
  failed += read_or_write(sc, basekey + "_byte_0123", [0, 1, 2, 3].pack("c*"), mode, true)
  
  return failed
end

def read_write_list(basekey, sc, mode)
  failed = 0
  
  failed += read_or_write(sc, basekey + "_list_empty", [], mode)
  failed += read_or_write(sc, basekey + "_list_0_1_2_3", [0, 1, 2, 3], mode)
  failed += read_or_write(sc, basekey + "_list_0_123_456_65000", [0, 123, 456, 65000], mode)
  failed += read_or_write(sc, basekey + "_list_0_123_456_0x10ffff", [0, 123, 456, 0x10ffff], mode)
  failed += read_or_write(sc, basekey + "_list_0_foo_1.5_false", [0, 'foo', 1.5, false], mode)
  # note: binary not supported in lists
  #failed += read_or_write(sc, basekey + "_list_0_foo_1.5_<<0123>>", [0, 'foo', 1.5, bytearray([0, 1, 2, 3])], mode)
  
  return failed
end

def read_write_map(basekey, sc, mode)
  failed = 0
  
  failed += read_or_write(sc, basekey + "_map_empty", {}, mode)
  failed += read_or_write(sc, basekey + "_map_x=0_y=1", {'x' => 0, 'y' => 1}, mode)
  failed += read_or_write(sc, basekey + "_map_a=0_b=foo_c=1.5_d=foo<nl>bar_e=list0123_f=mapx0y1",
    {'a' => 0, 'b' => 'foo', 'c' => 1.5, 'd' => "foo\nbar", 'e' => [0, 1, 2, 3], 'f' => {'x' => 0, 'y' => 1}}, mode)
  failed += read_or_write(sc, basekey + "_map_=0", {'' => 0}, mode)
  failed += read_or_write(sc, basekey + '_map_x=0_y=foo' + [0x0180, 0x01E3, 0x11E5].pack("U*"),
    {'x' => 0, 'y' => 'foo' + [0x0180, 0x01E3, 0x11E5].pack("U*")}, mode)
  failed += read_or_write(sc, basekey + '_map_x=0_foo' + [0x0180, 0x01E3, 0x11E5].pack("U*") + '=1',
    {'x' => 0, 'foo' + [0x0180, 0x01E3, 0x11E5].pack("U*") => 1}, mode)
  
  return failed
end

if (ARGV[0] == "read")
  basekey = ARGV[1]
  language = ARGV[2]
  basekey += '_' + language
  mode = :read
  puts 'Ruby-JSON-API: reading from ' + language
elsif (ARGV[0] == "write")
  basekey = ARGV[1]
  basekey += '_json_ruby'
  mode = :write
  puts 'Python-JSON-API: writing values'
else
  puts 'unknown commands: ' + ARGV.to_s
  exit 1
end

sc = Scalaris::TransactionSingleOp.new

failed = 0
failed += read_write_boolean(basekey, sc, mode)
failed += read_write_integer(basekey, sc, mode)
failed += read_write_long(basekey, sc, mode)
failed += read_write_biginteger(basekey, sc, mode)
failed += read_write_double(basekey, sc, mode)
failed += read_write_string(basekey, sc, mode)
failed += read_write_binary(basekey, sc, mode)
failed += read_write_list(basekey, sc, mode)
failed += read_write_map(basekey, sc, mode)
puts ''

if (failed > 0):
  puts failed.to_s + ' number of ' + mode.to_s + 's failed.'
  puts ''
  exit 1
end
Something went wrong with that request. Please try again.