|
| 1 | +# This file is a part of Julia. License is MIT: https://julialang.org/license |
| 2 | + |
| 3 | +using Test |
| 4 | + |
| 5 | +module ThereAreNoInvalidations |
| 6 | + function julia_expr_cmd(expr::Expr) |
| 7 | + `$(Base.julia_cmd()) -e $expr` |
| 8 | + end |
| 9 | + function readline_julia_expr_cmd(expr::Expr) |
| 10 | + open(readline, julia_expr_cmd(expr)) |
| 11 | + end |
| 12 | + function invalidations_expr(expr::Expr) |
| 13 | + quote |
| 14 | + try |
| 15 | + let invs = ccall(:jl_debug_method_invalidation, Any, (Cint,), 1) |
| 16 | + @eval $expr |
| 17 | + invs |
| 18 | + end |
| 19 | + finally |
| 20 | + ccall(:jl_debug_method_invalidation, Any, (Cint,), 0) |
| 21 | + end |
| 22 | + end |
| 23 | + end |
| 24 | + function there_are_no_invalidations_expr(expr::Expr) |
| 25 | + e = invalidations_expr(expr) |
| 26 | + quote |
| 27 | + let invalidations = $e |
| 28 | + show(isempty(invalidations)) |
| 29 | + end |
| 30 | + end |
| 31 | + end |
| 32 | + function there_are_no_invalidations(expr::Expr) |
| 33 | + e = there_are_no_invalidations_expr(expr) |
| 34 | + s = readline_julia_expr_cmd(e) |
| 35 | + parse(Bool, s) |
| 36 | + end |
| 37 | +end |
| 38 | + |
| 39 | +function type_expr(f, supertype::Type) |
| 40 | + type_name = :T |
| 41 | + rest = f(type_name) |
| 42 | + quote |
| 43 | + struct $type_name <: $supertype end |
| 44 | + $rest |
| 45 | + end |
| 46 | +end |
| 47 | + |
| 48 | +function test_expr(f, supertype::Type) |
| 49 | + e = type_expr(f, supertype) |
| 50 | + ThereAreNoInvalidations.there_are_no_invalidations(e) |
| 51 | +end |
| 52 | + |
| 53 | +@testset "no invalidations test set" begin |
| 54 | + concrete_subtypes_integer = (Bool, Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, BigInt) |
| 55 | + abstract_subtypes_number = (Signed, Unsigned, Integer, AbstractFloat, AbstractIrrational, Real, Number) |
| 56 | + @testset "2-arg `show` for various new types" begin |
| 57 | + stypes = (Any, AbstractString, Function, abstract_subtypes_number...) |
| 58 | + broken = (AbstractString, Function) |
| 59 | + @testset "S: $S" for S ∈ stypes |
| 60 | + @test test_expr((n -> :(function Base.show(::IO, ::$n) end)), S) broken=(S ∈ broken) |
| 61 | + end |
| 62 | + end |
| 63 | + @testset "new subtype of `AbstractString`" begin |
| 64 | + @testset "`print`" begin |
| 65 | + @test test_expr((n -> :(function Base.print(::IO, ::$n) end)), AbstractString) broken=true |
| 66 | + end |
| 67 | + @testset "index-related" begin |
| 68 | + fs = (thisind, prevind, nextind) |
| 69 | + broken = (thisind, prevind, nextind) |
| 70 | + @testset "f: $f" for f ∈ fs |
| 71 | + @test test_expr((n -> :(function Base.$f(::$n, ::Int) end)), AbstractString) broken=(f ∈ broken) |
| 72 | + end |
| 73 | + end |
| 74 | + end |
| 75 | + @testset "new subtype of `Integer`" begin |
| 76 | + @testset "construction of old int type from new type" begin |
| 77 | + broken = (UInt16, Int, UInt) |
| 78 | + (Int === Int64) && (broken = (broken..., Int32)) |
| 79 | + @testset "T: $T" for T ∈ concrete_subtypes_integer |
| 80 | + @test test_expr((n -> :(function Base.$T(::$n) end)), Integer) broken=(T ∈ broken) |
| 81 | + end |
| 82 | + end |
| 83 | + @testset "construction of new int type from old type" begin |
| 84 | + broken = () |
| 85 | + @testset "T: $T" for T ∈ concrete_subtypes_integer |
| 86 | + @test test_expr((n -> :(function $n(::$T) end)), Integer) broken=(T ∈ broken) |
| 87 | + end |
| 88 | + end |
| 89 | + @testset "arithmetic between old int types and new int type" begin |
| 90 | + ops = (:+, :*, :<<, :>>, :>>>) |
| 91 | + broken = () |
| 92 | + @testset "T: $T" for T ∈ concrete_subtypes_integer |
| 93 | + @testset "op: $op" for op ∈ ops |
| 94 | + @test ( |
| 95 | + test_expr((n -> :(function Base.$op(::$n, ::$T) end)), Integer) && |
| 96 | + test_expr((n -> :(function Base.$op(::$T, ::$n) end)), Integer) |
| 97 | + ) broken=((T, op) ∈ broken) |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | + @testset "promotion between old int types and new int type" begin |
| 102 | + broken = (Bool, UInt8, Int) |
| 103 | + @testset "T: $T" for T ∈ concrete_subtypes_integer |
| 104 | + @test ( |
| 105 | + test_expr((n -> :(function Base.promote_rule(::Type{$T}, ::Type{$n}) end)), Integer) && |
| 106 | + test_expr((n -> :(function Base.promote_rule(::Type{$n}, ::Type{$T}) end)), Integer) |
| 107 | + ) broken=(T ∈ broken) |
| 108 | + end |
| 109 | + end |
| 110 | + @testset "unary functions" begin |
| 111 | + fs = (zero, one) |
| 112 | + broken = (zero, one) |
| 113 | + @testset "f: $f" for f ∈ fs |
| 114 | + @test test_expr((n -> :(function Base.$f(::$n) end)), Integer) broken=(f ∈ broken) |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | +end |
0 commit comments