From be1c7e50e1e8809ea56f2c9d472eccd8ffd73a97 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 04:57:58 +0200 Subject: Adding upstream version 1.44.3. Signed-off-by: Daniel Baumann --- .../deps/mruby/mrbgems/mruby-hash-ext/mrbgem.rake | 8 + .../mruby/mrbgems/mruby-hash-ext/mrblib/hash.rb | 477 +++++++++++++++++++++ .../mruby/mrbgems/mruby-hash-ext/src/hash-ext.c | 88 ++++ .../deps/mruby/mrbgems/mruby-hash-ext/test/hash.rb | 294 +++++++++++++ 4 files changed, 867 insertions(+) create mode 100644 web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-hash-ext/mrbgem.rake create mode 100644 web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-hash-ext/mrblib/hash.rb create mode 100644 web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-hash-ext/src/hash-ext.c create mode 100644 web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-hash-ext/test/hash.rb (limited to 'web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-hash-ext') diff --git a/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-hash-ext/mrbgem.rake b/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-hash-ext/mrbgem.rake new file mode 100644 index 00000000..103410ab --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-hash-ext/mrbgem.rake @@ -0,0 +1,8 @@ +MRuby::Gem::Specification.new('mruby-hash-ext') do |spec| + spec.license = 'MIT' + spec.author = 'mruby developers' + spec.summary = 'Hash class extension' + spec.add_dependency 'mruby-enum-ext', core: 'mruby-enum-ext' + spec.add_dependency 'mruby-array-ext', core: 'mruby-array-ext' + spec.add_test_dependency 'mruby-enumerator', core: 'mruby-enumerator' +end diff --git a/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-hash-ext/mrblib/hash.rb b/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-hash-ext/mrblib/hash.rb new file mode 100644 index 00000000..73d1fbe6 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-hash-ext/mrblib/hash.rb @@ -0,0 +1,477 @@ +class Hash + + # ISO does not define Hash#each_pair, so each_pair is defined in gem. + alias each_pair each + + ## + # call-seq: + # Hash[ key, value, ... ] -> new_hash + # Hash[ [ [key, value], ... ] ] -> new_hash + # Hash[ object ] -> new_hash + # + # Creates a new hash populated with the given objects. + # + # Similar to the literal `{ _key_ => _value_, ... }`. In the first + # form, keys and values occur in pairs, so there must be an even number of + # arguments. + # + # The second and third form take a single argument which is either an array + # of key-value pairs or an object convertible to a hash. + # + # Hash["a", 100, "b", 200] #=> {"a"=>100, "b"=>200} + # Hash[ [ ["a", 100], ["b", 200] ] ] #=> {"a"=>100, "b"=>200} + # Hash["a" => 100, "b" => 200] #=> {"a"=>100, "b"=>200} + # + + def self.[](*object) + length = object.length + if length == 1 + o = object[0] + if o.respond_to?(:to_hash) + h = self.new + object[0].to_hash.each { |k, v| h[k] = v } + return h + elsif o.respond_to?(:to_a) + h = self.new + o.to_a.each do |i| + raise ArgumentError, "wrong element type #{i.class} (expected array)" unless i.respond_to?(:to_a) + k, v = nil + case i.size + when 2 + k = i[0] + v = i[1] + when 1 + k = i[0] + else + raise ArgumentError, "invalid number of elements (#{i.size} for 1..2)" + end + h[k] = v + end + return h + end + end + unless length % 2 == 0 + raise ArgumentError, 'odd number of arguments for Hash' + end + h = self.new + 0.step(length - 2, 2) do |i| + h[object[i]] = object[i + 1] + end + h + end + + ## + # call-seq: + # Hash.try_convert(obj) -> hash or nil + # + # Try to convert obj into a hash, using to_hash method. + # Returns converted hash or nil if obj cannot be converted + # for any reason. + # + # Hash.try_convert({1=>2}) # => {1=>2} + # Hash.try_convert("1=>2") # => nil + # + def self.try_convert(obj) + if obj.respond_to?(:to_hash) + obj.to_hash + else + nil + end + end + + ## + # call-seq: + # hsh.merge!(other_hash) -> hsh + # hsh.merge!(other_hash){|key, oldval, newval| block} -> hsh + # + # Adds the contents of _other_hash_ to _hsh_. If no block is specified, + # entries with duplicate keys are overwritten with the values from + # _other_hash_, otherwise the value of each duplicate key is determined by + # calling the block with the key, its value in _hsh_ and its value in + # _other_hash_. + # + # h1 = { "a" => 100, "b" => 200 } + # h2 = { "b" => 254, "c" => 300 } + # h1.merge!(h2) #=> {"a"=>100, "b"=>254, "c"=>300} + # + # h1 = { "a" => 100, "b" => 200 } + # h2 = { "b" => 254, "c" => 300 } + # h1.merge!(h2) { |key, v1, v2| v1 } + # #=> {"a"=>100, "b"=>200, "c"=>300} + # + + def merge!(other, &block) + raise TypeError, "can't convert argument into Hash" unless other.respond_to?(:to_hash) + if block + other.each_key{|k| + self[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k] + } + else + other.each_key{|k| self[k] = other[k]} + end + self + end + + alias update merge! + + ## + # call-seq: + # hsh.compact -> new_hsh + # + # Returns a new hash with the nil values/key pairs removed + # + # h = { a: 1, b: false, c: nil } + # h.compact #=> { a: 1, b: false } + # h #=> { a: 1, b: false, c: nil } + # + def compact + result = self.dup + result.compact! + result + end + + ## + # call-seq: + # hsh.fetch(key [, default] ) -> obj + # hsh.fetch(key) {| key | block } -> obj + # + # Returns a value from the hash for the given key. If the key can't be + # found, there are several options: With no other arguments, it will + # raise an KeyError exception; if default is + # given, then that will be returned; if the optional code block is + # specified, then that will be run and its result returned. + # + # h = { "a" => 100, "b" => 200 } + # h.fetch("a") #=> 100 + # h.fetch("z", "go fish") #=> "go fish" + # h.fetch("z") { |el| "go fish, #{el}"} #=> "go fish, z" + # + # The following example shows that an exception is raised if the key + # is not found and a default value is not supplied. + # + # h = { "a" => 100, "b" => 200 } + # h.fetch("z") + # + # produces: + # + # prog.rb:2:in 'fetch': key not found (KeyError) + # from prog.rb:2 + # + + def fetch(key, none=NONE, &block) + unless self.key?(key) + if block + block.call(key) + elsif none != NONE + none + else + raise KeyError, "Key not found: #{key}" + end + else + self[key] + end + end + + ## + # call-seq: + # hsh.delete_if {| key, value | block } -> hsh + # hsh.delete_if -> an_enumerator + # + # Deletes every key-value pair from hsh for which block + # evaluates to true. + # + # If no block is given, an enumerator is returned instead. + # + # h = { "a" => 100, "b" => 200, "c" => 300 } + # h.delete_if {|key, value| key >= "b" } #=> {"a"=>100} + # + + def delete_if(&block) + return to_enum :delete_if unless block + + self.each do |k, v| + self.delete(k) if block.call(k, v) + end + self + end + + ## + # call-seq: + # hash.flatten -> an_array + # hash.flatten(level) -> an_array + # + # Returns a new array that is a one-dimensional flattening of this + # hash. That is, for every key or value that is an array, extract + # its elements into the new array. Unlike Array#flatten, this + # method does not flatten recursively by default. The optional + # level argument determines the level of recursion to flatten. + # + # a = {1=> "one", 2 => [2,"two"], 3 => "three"} + # a.flatten # => [1, "one", 2, [2, "two"], 3, "three"] + # a.flatten(2) # => [1, "one", 2, 2, "two", 3, "three"] + # + + def flatten(level=1) + self.to_a.flatten(level) + end + + ## + # call-seq: + # hsh.invert -> new_hash + # + # Returns a new hash created by using hsh's values as keys, and + # the keys as values. + # + # h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 } + # h.invert #=> {0=>"a", 100=>"m", 200=>"d", 300=>"y"} + # + + def invert + h = self.class.new + self.each {|k, v| h[v] = k } + h + end + + ## + # call-seq: + # hsh.keep_if {| key, value | block } -> hsh + # hsh.keep_if -> an_enumerator + # + # Deletes every key-value pair from hsh for which block + # evaluates to false. + # + # If no block is given, an enumerator is returned instead. + # + + def keep_if(&block) + return to_enum :keep_if unless block + + keys = [] + self.each do |k, v| + unless block.call([k, v]) + self.delete(k) + end + end + self + end + + ## + # call-seq: + # hsh.key(value) -> key + # + # Returns the key of an occurrence of a given value. If the value is + # not found, returns nil. + # + # h = { "a" => 100, "b" => 200, "c" => 300, "d" => 300 } + # h.key(200) #=> "b" + # h.key(300) #=> "c" + # h.key(999) #=> nil + # + + def key(val) + self.each do |k, v| + return k if v == val + end + nil + end + + ## + # call-seq: + # hsh.to_h -> hsh or new_hash + # + # Returns +self+. If called on a subclass of Hash, converts + # the receiver to a Hash object. + # + def to_h + self + end + + ## + # call-seq: + # hash < other -> true or false + # + # Returns true if hash is subset of + # other. + # + # h1 = {a:1, b:2} + # h2 = {a:1, b:2, c:3} + # h1 < h2 #=> true + # h2 < h1 #=> false + # h1 < h1 #=> false + # + def <(hash) + begin + hash = hash.to_hash + rescue NoMethodError + raise TypeError, "can't convert #{hash.class} to Hash" + end + size < hash.size and all? {|key, val| + hash.key?(key) and hash[key] == val + } + end + + ## + # call-seq: + # hash <= other -> true or false + # + # Returns true if hash is subset of + # other or equals to other. + # + # h1 = {a:1, b:2} + # h2 = {a:1, b:2, c:3} + # h1 <= h2 #=> true + # h2 <= h1 #=> false + # h1 <= h1 #=> true + # + def <=(hash) + begin + hash = hash.to_hash + rescue NoMethodError + raise TypeError, "can't convert #{hash.class} to Hash" + end + size <= hash.size and all? {|key, val| + hash.key?(key) and hash[key] == val + } + end + + ## + # call-seq: + # hash > other -> true or false + # + # Returns true if other is subset of + # hash. + # + # h1 = {a:1, b:2} + # h2 = {a:1, b:2, c:3} + # h1 > h2 #=> false + # h2 > h1 #=> true + # h1 > h1 #=> false + # + def >(hash) + begin + hash = hash.to_hash + rescue NoMethodError + raise TypeError, "can't convert #{hash.class} to Hash" + end + size > hash.size and hash.all? {|key, val| + key?(key) and self[key] == val + } + end + + ## + # call-seq: + # hash >= other -> true or false + # + # Returns true if other is subset of + # hash or equals to hash. + # + # h1 = {a:1, b:2} + # h2 = {a:1, b:2, c:3} + # h1 >= h2 #=> false + # h2 >= h1 #=> true + # h1 >= h1 #=> true + # + def >=(hash) + begin + hash = hash.to_hash + rescue NoMethodError + raise TypeError, "can't convert #{hash.class} to Hash" + end + size >= hash.size and hash.all? {|key, val| + key?(key) and self[key] == val + } + end + + ## + # call-seq: + # hsh.dig(key,...) -> object + # + # Extracts the nested value specified by the sequence of key + # objects by calling +dig+ at each step, returning +nil+ if any + # intermediate step is +nil+. + # + def dig(idx,*args) + n = self[idx] + if args.size > 0 + n&.dig(*args) + else + n + end + end + + ## + # call-seq: + # hsh.transform_keys {|key| block } -> new_hash + # hsh.transform_keys -> an_enumerator + # + # Returns a new hash, with the keys computed from running the block + # once for each key in the hash, and the values unchanged. + # + # If no block is given, an enumerator is returned instead. + # + def transform_keys(&block) + return to_enum :transform_keys unless block + hash = {} + self.keys.each do |k| + new_key = block.call(k) + hash[new_key] = self[k] + end + hash + end + ## + # call-seq: + # hsh.transform_keys! {|key| block } -> hsh + # hsh.transform_keys! -> an_enumerator + # + # Invokes the given block once for each key in hsh, replacing it + # with the new key returned by the block, and then returns hsh. + # + # If no block is given, an enumerator is returned instead. + # + def transform_keys!(&block) + return to_enum :transform_keys! unless block + self.keys.each do |k| + value = self[k] + new_key = block.call(k) + self.__delete(k) + self[new_key] = value + end + self + end + ## + # call-seq: + # hsh.transform_values {|value| block } -> new_hash + # hsh.transform_values -> an_enumerator + # + # Returns a new hash with the results of running the block once for + # every value. + # This method does not change the keys. + # + # If no block is given, an enumerator is returned instead. + # + def transform_values(&b) + return to_enum :transform_values unless block_given? + hash = {} + self.keys.each do |k| + hash[k] = yield(self[k]) + end + hash + end + ## + # call-seq: + # hsh.transform_values! {|key| block } -> hsh + # hsh.transform_values! -> an_enumerator + # + # Invokes the given block once for each value in the hash, replacing + # with the new value returned by the block, and then returns hsh. + # + # If no block is given, an enumerator is returned instead. + # + def transform_values!(&b) + return to_enum :transform_values! unless block_given? + self.keys.each do |k| + self[k] = yield(self[k]) + end + self + end +end diff --git a/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-hash-ext/src/hash-ext.c b/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-hash-ext/src/hash-ext.c new file mode 100644 index 00000000..952f2eb6 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-hash-ext/src/hash-ext.c @@ -0,0 +1,88 @@ +/* +** hash.c - Hash class +** +** See Copyright Notice in mruby.h +*/ + +#include +#include +#include + +/* + * call-seq: + * hsh.values_at(key, ...) -> array + * + * Return an array containing the values associated with the given keys. + * Also see Hash.select. + * + * h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" } + * h.values_at("cow", "cat") #=> ["bovine", "feline"] + */ + +static mrb_value +hash_values_at(mrb_state *mrb, mrb_value hash) +{ + mrb_value *argv, result; + mrb_int argc, i; + int ai; + + mrb_get_args(mrb, "*", &argv, &argc); + result = mrb_ary_new_capa(mrb, argc); + ai = mrb_gc_arena_save(mrb); + for (i = 0; i < argc; i++) { + mrb_ary_push(mrb, result, mrb_hash_get(mrb, hash, argv[i])); + mrb_gc_arena_restore(mrb, ai); + } + return result; +} + +/* + * call-seq: + * hsh.compact! -> hsh + * + * Removes all nil values from the hash. Returns the hash. + * + * h = { a: 1, b: false, c: nil } + * h.compact! #=> { a: 1, b: false } + */ +static mrb_value +hash_compact_bang(mrb_state *mrb, mrb_value hash) +{ + khiter_t k; + khash_t(ht) *h = RHASH_TBL(hash); + mrb_int n = -1; + + if (!h) return mrb_nil_value(); + for (k = kh_begin(h); k != kh_end(h); k++) { + if (kh_exist(h, k)) { + mrb_value val = kh_value(h, k).v; + khiter_t k2; + + if (mrb_nil_p(val)) { + kh_del(ht, mrb, h, k); + n = kh_value(h, k).n; + for (k2 = kh_begin(h); k2 != kh_end(h); k2++) { + if (!kh_exist(h, k2)) continue; + if (kh_value(h, k2).n > n) kh_value(h, k2).n--; + } + } + } + } + if (n < 0) return mrb_nil_value(); + return hash; +} + +void +mrb_mruby_hash_ext_gem_init(mrb_state *mrb) +{ + struct RClass *h; + + h = mrb->hash_class; + mrb_define_method(mrb, h, "values_at", hash_values_at, MRB_ARGS_ANY()); + mrb_define_method(mrb, h, "compact!", hash_compact_bang, MRB_ARGS_NONE()); +} + +void +mrb_mruby_hash_ext_gem_final(mrb_state *mrb) +{ +} diff --git a/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-hash-ext/test/hash.rb b/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-hash-ext/test/hash.rb new file mode 100644 index 00000000..ca4e346f --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-hash-ext/test/hash.rb @@ -0,0 +1,294 @@ +## +# Hash(Ext) Test + +assert('Hash.[] Hash') do + a = Hash['a_key' => 'a_value'] + + assert_equal({'a_key' => 'a_value'}, a) +end + +assert('Hash.[] [ [ ["b_key", "b_value" ] ] ]') do + a = Hash[ [ ['b_key', 'b_value'] ] ] + + assert_equal({'b_key' => 'b_value'}, a) + + a = Hash[ [ ] ] + + assert_equal({}, a) + + assert_raise(ArgumentError) do + Hash[ [ ['b_key', 'b_value', 'b_over'] ] ] + end + + assert_raise(ArgumentError) do + Hash[ [ [] ] ] + end +end + +assert('Hash.[] "c_key", "c_value"') do + a = Hash['c_key', 'c_value', 'd_key', 1] + + assert_equal({'c_key' => 'c_value', 'd_key' => 1}, a) + + a = Hash[] + + assert_equal({}, a) + + assert_raise(ArgumentError) do + Hash['d_key'] + end +end + +assert('Hash.[] for sub class') do + sub_hash_class = Class.new(Hash) + sub_hash = sub_hash_class[] + assert_equal(sub_hash_class, sub_hash.class) +end + +assert('Hash.try_convert') do + assert_nil Hash.try_convert(nil) + assert_nil Hash.try_convert("{1=>2}") + assert_equal({1=>2}, Hash.try_convert({1=>2})) +end + +assert('Hash#merge!') do + a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' } + b = { 'cba_key' => 'XXX', 'xyz_key' => 'xyz_value' } + + result_1 = a.merge! b + + a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' } + result_2 = a.merge!(b) do |key, original, new| + original + end + + assert_equal({'abc_key' => 'abc_value', 'cba_key' => 'XXX', + 'xyz_key' => 'xyz_value' }, result_1) + assert_equal({'abc_key' => 'abc_value', 'cba_key' => 'cba_value', + 'xyz_key' => 'xyz_value' }, result_2) + + assert_raise(TypeError) do + { 'abc_key' => 'abc_value' }.merge! "a" + end +end + +assert('Hash#values_at') do + h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" } + assert_equal ["bovine", "feline"], h.values_at("cow", "cat") + + keys = [] + (0...1000).each { |v| keys.push "#{v}" } + h = Hash.new { |hash,k| hash[k] = k } + assert_equal keys, h.values_at(*keys) +end + +assert('Hash#compact') do + h = { "cat" => "feline", "dog" => nil, "cow" => false } + + assert_equal({ "cat" => "feline", "cow" => false }, h.compact) + assert_equal({ "cat" => "feline", "dog" => nil, "cow" => false }, h) +end + +assert('Hash#compact!') do + h = { "cat" => "feline", "dog" => nil, "cow" => false } + + h.compact! + assert_equal({ "cat" => "feline", "cow" => false }, h) +end + +assert('Hash#fetch') do + h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" } + assert_equal "feline", h.fetch("cat") + assert_equal "mickey", h.fetch("mouse", "mickey") + assert_equal "minny", h.fetch("mouse"){"minny"} + assert_equal "mouse", h.fetch("mouse"){|k| k} + assert_raise(KeyError) do + h.fetch("gnu") + end +end + +assert("Hash#delete_if") do + base = { 1 => 'one', 2 => false, true => 'true', 'cat' => 99 } + h1 = { 1 => 'one', 2 => false, true => 'true' } + h2 = { 2 => false, 'cat' => 99 } + h3 = { 2 => false } + + h = base.dup + assert_equal(h, h.delete_if { false }) + assert_equal({}, h.delete_if { true }) + + h = base.dup + assert_equal(h1, h.delete_if {|k,v| k.instance_of?(String) }) + assert_equal(h1, h) + + h = base.dup + assert_equal(h2, h.delete_if {|k,v| v.instance_of?(String) }) + assert_equal(h2, h) + + h = base.dup + assert_equal(h3, h.delete_if {|k,v| v }) + assert_equal(h3, h) + + h = base.dup + n = 0 + h.delete_if {|*a| + n += 1 + assert_equal(2, a.size) + assert_equal(base[a[0]], a[1]) + h.shift + true + } + assert_equal(base.size, n) +end + +assert("Hash#flatten") do + a = {1=> "one", 2 => [2,"two"], 3 => [3, ["three"]]} + assert_equal [1, "one", 2, [2, "two"], 3, [3, ["three"]]], a.flatten + assert_equal [[1, "one"], [2, [2, "two"]], [3, [3, ["three"]]]], a.flatten(0) + assert_equal [1, "one", 2, [2, "two"], 3, [3, ["three"]]], a.flatten(1) + assert_equal [1, "one", 2, 2, "two", 3, 3, ["three"]], a.flatten(2) + assert_equal [1, "one", 2, 2, "two", 3, 3, "three"], a.flatten(3) +end + +assert("Hash#invert") do + h = { 1 => 'one', 2 => 'two', 3 => 'three', + true => 'true', nil => 'nil' }.invert + assert_equal 1, h['one'] + assert_equal true, h['true'] + assert_equal nil, h['nil'] + + h = { 'a' => 1, 'b' => 2, 'c' => 1 }.invert + assert_equal(2, h.length) + assert_include(%w[a c], h[1]) + assert_equal('b', h[2]) +end + +assert("Hash#invert with sub class") do + sub_hash_class = Class.new(Hash) + sub_hash = sub_hash_class.new + assert_equal(sub_hash_class, sub_hash.invert.class) +end + +assert("Hash#keep_if") do + h = { 1 => 2, 3 => 4, 5 => 6 } + assert_equal({3=>4,5=>6}, h.keep_if {|k, v| k + v >= 7 }) + h = { 1 => 2, 3 => 4, 5 => 6 } + assert_equal({ 1 => 2, 3=> 4, 5 =>6} , h.keep_if { true }) +end + +assert("Hash#key") do + h = { "a" => 100, "b" => 200, "c" => 300, "d" => 300, nil => 'nil', 'nil' => nil } + assert_equal "b", h.key(200) + assert_equal "c", h.key(300) + assert_nil h.key(999) + assert_nil h.key('nil') + assert_equal 'nil', h.key(nil) +end + +assert("Hash#to_h") do + h = { "a" => 100, "b" => 200 } + assert_equal Hash, h.to_h.class + assert_equal h, h.to_h +end + +assert('Hash#<') do + h1 = {a:1, b:2} + h2 = {a:1, b:2, c:3} + + assert_false(h1 < h1) + assert_true(h1 < h2) + assert_false(h2 < h1) + assert_false(h2 < h2) + + h1 = {a:1} + h2 = {a:2} + + assert_false(h1 < h1) + assert_false(h1 < h2) + assert_false(h2 < h1) + assert_false(h2 < h2) +end + +assert('Hash#<=') do + h1 = {a:1, b:2} + h2 = {a:1, b:2, c:3} + + assert_true(h1 <= h1) + assert_true(h1 <= h2) + assert_false(h2 <= h1) + assert_true(h2 <= h2) + + h1 = {a:1} + h2 = {a:2} + + assert_true(h1 <= h1) + assert_false(h1 <= h2) + assert_false(h2 <= h1) + assert_true(h2 <= h2) +end + +assert('Hash#>=') do + h1 = {a:1, b:2} + h2 = {a:1, b:2, c:3} + + assert_true(h1 >= h1) + assert_false(h1 >= h2) + assert_true(h2 >= h1) + assert_true(h2 >= h2) + + h1 = {a:1} + h2 = {a:2} + + assert_true(h1 >= h1) + assert_false(h1 >= h2) + assert_false(h2 >= h1) + assert_true(h2 >= h2) +end + +assert('Hash#>') do + h1 = {a:1, b:2} + h2 = {a:1, b:2, c:3} + + assert_false(h1 > h1) + assert_false(h1 > h2) + assert_true(h2 > h1) + assert_false(h2 > h2) + + h1 = {a:1} + h2 = {a:2} + + assert_false(h1 > h1) + assert_false(h1 > h2) + assert_false(h2 > h1) + assert_false(h2 > h2) +end + +assert("Hash#dig") do + h = {a:{b:{c:1}}} + assert_equal(1, h.dig(:a, :b, :c)) + assert_nil(h.dig(:d)) +end + +assert("Hash#transform_keys") do + h = {"1" => 100, "2" => 200} + assert_equal(h.transform_keys{|k| k+"!"}, + {"1!" => 100, "2!" => 200}) + assert_equal(h.transform_keys{|k|k.to_i}, + {1 => 100, 2 => 200}) + assert_equal(h.transform_keys.with_index{|k, i| "#{k}.#{i}"}, + {"1.0" => 100, "2.1" => 200}) + assert_equal(h.transform_keys!{|k|k.to_i}, h) + assert_equal(h, {1 => 100, 2 => 200}) +end + +assert("Hash#transform_values") do + h = {a: 1, b: 2, c: 3} + assert_equal(h.transform_values{|v| v * v + 1}, + {a: 2, b: 5, c: 10}) + assert_equal(h.transform_values{|v|v.to_s}, + {a: "1", b: "2", c: "3"}) + assert_equal(h.transform_values.with_index{|v, i| "#{v}.#{i}"}, + {a: "1.0", b: "2.1", c: "3.2"}) + assert_equal(h.transform_values!{|v|v.to_s}, h) + assert_equal(h, {a: "1", b: "2", c: "3"}) +end -- cgit v1.2.3