desc: Tests randomization functions tests: # Test sample - cd: r.expr([1,2,3]).sample(3).distinct().count() ot: 3 - cd: r.expr([1,2,3]).sample(3).count() ot: 3 - cd: r.expr([1,2,3,4,5,6]).sample(3).distinct().count() ot: 3 - cd: r.expr([1,2,3]).sample(4).distinct().count() ot: 3 - rb: r.expr([[1,2,3], 2]).do{|x| x[0].sample(x[1])}.distinct().count() ot: 2 - cd: r.expr([1,2,3]).sample(-1) ot: err('ReqlQueryLogicError', 'Number of items to sample must be non-negative, got `-1`.', [0]) - cd: r.expr(1).sample(1) ot: err('ReqlQueryLogicError', 'Cannot convert NUMBER to SEQUENCE', [0]) - cd: r.expr({}).sample(1) ot: err('ReqlQueryLogicError', 'Cannot convert OBJECT to SEQUENCE', [0]) # Test r.random with floating-point values # These expressions should be equivalent - py: - r.random().do(lambda x:r.and_(x.ge(0), x.lt(1))) - r.random(1, float=True).do(lambda x:r.and_(x.ge(0), x.lt(1))) - r.random(0, 1, float=True).do(lambda x:r.and_(x.ge(0), x.lt(1))) - r.random(1, 0, float=True).do(lambda x:r.and_(x.le(1), x.gt(0))) - r.random(r.expr(0), 1, float=True).do(lambda x:r.and_(x.ge(0), x.lt(1))) - r.random(1, r.expr(0), float=True).do(lambda x:r.and_(x.le(1), x.gt(0))) - r.random(r.expr(1), r.expr(0), float=True).do(lambda x:r.and_(x.le(1), x.gt(0))) ot: True # Single-argument - py: - r.random(0.495, float=True).do(lambda x:r.and_(x.ge(0), x.lt(0.495))) - r.random(-0.495, float=True).do(lambda x:r.and_(x.le(0), x.gt(-0.495))) - r.random(1823756.24, float=True).do(lambda x:r.and_(x.ge(0), x.lt(1823756.24))) - r.random(-1823756.24, float=True).do(lambda x:r.and_(x.le(0), x.gt(-1823756.24))) ot: True # Non-zero-based random numbers - py: - r.random(10.5, 20.153, float=True).do(lambda x:r.and_(x.ge(10.5), x.lt(20.153))) - r.random(20.153, 10.5, float=True).do(lambda x:r.and_(x.le(20.153), x.gt(10.5))) - r.random(31415926.1, 31415926, float=True).do(lambda x:r.and_(x.le(31415926.1), x.gt(31415926))) ot: True # Negative random numbers - py: - r.random(-10.5, 20.153, float=True).do(lambda x:r.and_(x.ge(-10.5), x.lt(20.153))) - r.random(-20.153, -10.5, float=True).do(lambda x:r.and_(x.ge(-20.153), x.lt(-10.5))) - r.random(-31415926, -31415926.1, float=True).do(lambda x:r.and_(x.le(-31415926), x.gt(-31415926.1))) ot: True # There is a very small chance of collision here - py: - r.expr([r.random(), r.random()]).distinct().count() - r.expr([r.random(1, float=True), r.random(1, float=True)]).distinct().count() - r.expr([r.random(0, 1, float=True), r.random(0, 1, float=True)]).distinct().count() ot: 2 # Zero range random - py: - r.random(0, float=True).eq(0) - r.random(5, 5, float=True).eq(5) - r.random(-499384756758, -499384756758, float=True).eq(-499384756758) - r.random(-93.94757, -93.94757, float=True).eq(-93.94757) - r.random(294.69148, 294.69148, float=True).eq(294.69148) ot: True # Test limits of doubles - def: py: float_max = sys.float_info.max js: float_max = Number.MAX_VALUE rb: float_max = Float::MAX - def: py: float_min = sys.float_info.min js: float_min = Number.MIN_VALUE rb: float_min = Float::MIN - py: - r.random(-float_max, float_max, float=True).do(lambda x:r.and_(x.ge(-float_max), x.lt(float_max))) - r.random(float_max, -float_max, float=True).do(lambda x:r.and_(x.le(float_max), x.gt(-float_max))) - r.random(float_min, float_max, float=True).do(lambda x:r.and_(x.ge(float_min), x.lt(float_max))) - r.random(float_min, -float_max, float=True).do(lambda x:r.and_(x.le(float_min), x.gt(-float_max))) - r.random(-float_min, float_max, float=True).do(lambda x:r.and_(x.ge(-float_min), x.lt(float_max))) - r.random(-float_min, -float_max, float=True).do(lambda x:r.and_(x.le(-float_min), x.gt(-float_max))) ot: True # Test r.random with integer values - def: py: upper_limit = 2**53 - 1 js: upper_limit = Math.pow(2,53) - 1 rb: upper_limit = 2**53 - 1 - def: py: lower_limit = 1 - (2**53) js: lower_limit = 1 - Math.pow(2,53) rb: lower_limit = 1 - (2**53) # These expressions should be equivalent - py: - r.random(256).do(lambda x:r.and_(x.ge(0), x.lt(256))) - r.random(0, 256).do(lambda x:r.and_(x.ge(0), x.lt(256))) - r.random(r.expr(256)).do(lambda x:r.and_(x.ge(0), x.lt(256))) - r.random(r.expr(0), 256).do(lambda x:r.and_(x.ge(0), x.lt(256))) - r.random(0, r.expr(256)).do(lambda x:r.and_(x.ge(0), x.lt(256))) - r.random(r.expr(0), r.expr(256)).do(lambda x:r.and_(x.ge(0), x.lt(256))) ot: True # Non-zero-based random numbers - py: - r.random(10, 20).do(lambda x:r.and_(x.ge(10), x.lt(20))) - r.random(9347849, 120937493).do(lambda x:r.and_(x.ge(9347849), x.lt(120937493))) js: - r.random(10, 20).do(function(x){return r.and(x.ge(10), x.lt(20))}) - r.random(9347849, 120937493).do(function(x){return r.and(x.ge(9347849), x.lt(120937493))}) rb: - r.random(10, 20).do{|x| r.and(x.ge(10), x.lt(20))} - r.random(9347849, 120937493).do{|x| r.and(x.ge(9347849), x.lt(120937493))} ot: True # Negative random numbers - py: - r.random(-10, 20).do(lambda x:r.and_(x.ge(-10), x.lt(20))) - r.random(-20, -10).do(lambda x:r.and_(x.ge(-20), x.lt(-10))) - r.random(-120937493, -9347849).do(lambda x:r.and_(x.ge(-120937493), x.lt(-9347849))) js: - r.random(-10, 20).do(function(x){return r.and(x.ge(-10), x.lt(20))}) - r.random(-20, -10).do(function(x){return r.and(x.ge(-20), x.lt(-10))}) - r.random(-120937493, -9347849).do(function(x){return r.and(x.ge(-120937493), x.lt(-9347849))}) rb: - r.random(-10, 20).do{|x| r.and(x.ge(-10), x.lt(20))} - r.random(-20, -10).do{|x| r.and(x.ge(-20), x.lt(-10))} - r.random(-120937493, -9347849).do{|x| r.and(x.ge(-120937493), x.lt(-9347849))} ot: True # There is a very small chance of collision here - cd: r.expr([r.random(upper_limit), r.random(upper_limit)]).distinct().count() ot: 2 - py: r.expr([upper_limit,upper_limit]).map(lambda x:r.random(x)).distinct().count() js: r.expr([upper_limit,upper_limit]).map(function(x){return r.random(x)}).distinct().count() rb: r.expr([upper_limit,upper_limit]).map{|x| r.random(x)}.distinct().count() ot: 2 # Error cases # Non-integer limits - cd: r.random(-0.5) ot: err("ReqlQueryLogicError", "Upper bound (-0.5) could not be safely converted to an integer.", []) - cd: r.random(0.25) ot: err("ReqlQueryLogicError", "Upper bound (0.25) could not be safely converted to an integer.", []) - cd: r.random(-10, 0.75) ot: err("ReqlQueryLogicError", "Upper bound (0.75) could not be safely converted to an integer.", []) - cd: r.random(-120549.25, 39458) ot: err("ReqlQueryLogicError", "Lower bound (-120549.25) could not be safely converted to an integer.", []) - cd: r.random(-6.5, 8.125) ot: err("ReqlQueryLogicError", "Lower bound (-6.5) could not be safely converted to an integer.", []) # Forced integer random with no bounds - py: r.random(float=False) js: r.random({float:false}) rb: r.random({:float => false}) ot: err("ReqlQueryLogicError", "Generating a random integer requires one or two bounds.", []) # Lower bound not less than upper bound - cd: r.random(0) ot: err("ReqlQueryLogicError", "Lower bound (0) is not less than upper bound (0).", []) - cd: r.random(0, 0) ot: err("ReqlQueryLogicError", "Lower bound (0) is not less than upper bound (0).", []) - cd: r.random(515, 515) ot: err("ReqlQueryLogicError", "Lower bound (515) is not less than upper bound (515).", []) - cd: r.random(-956, -956) ot: err("ReqlQueryLogicError", "Lower bound (-956) is not less than upper bound (-956).", []) - cd: r.random(-10) ot: err("ReqlQueryLogicError", "Lower bound (0) is not less than upper bound (-10).", []) - cd: r.random(20, 2) ot: err("ReqlQueryLogicError", "Lower bound (20) is not less than upper bound (2).", []) - cd: r.random(2, -20) ot: err("ReqlQueryLogicError", "Lower bound (2) is not less than upper bound (-20).", []) - cd: r.random(1456, 0) ot: err("ReqlQueryLogicError", "Lower bound (1456) is not less than upper bound (0).", [])