"Fossies" - the Fresh Open Source Software Archive

Member "julia-1.1.1/share/julia/test/char.jl" (16 May 2019, 8677 Bytes) of package /linux/misc/julia-1.1.1-linux-i686.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Julia source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # This file is a part of Julia. License is MIT: https://julialang.org/license
    2 
    3 @testset "basic properties" begin
    4 
    5     @test typemin(Char) == Char(0)
    6     @test ndims(Char) == 0
    7     @test getindex('a', 1) == 'a'
    8     @test_throws BoundsError getindex('a', 2)
    9     # This is current behavior, but it seems questionable
   10     @test getindex('a', 1, 1, 1) == 'a'
   11     @test_throws BoundsError getindex('a', 1, 1, 2)
   12 
   13     @test 'b' + 1 == 'c'
   14     @test typeof('b' + 1) == Char
   15     @test 1 + 'b' == 'c'
   16     @test typeof(1 + 'b') == Char
   17     @test 'b' - 1 == 'a'
   18     @test typeof('b' - 1) == Char
   19 
   20     @test widen('a') === 'a'
   21     # just check this works
   22     @test_throws Base.CodePointError Base.code_point_err(UInt32(1))
   23 end
   24 
   25 @testset "ASCII conversion to/from Integer" begin
   26     numberchars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
   27     lowerchars = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
   28     upperchars = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
   29     plane1_playingcards = ['🂠', '🂡', '🂢', '🂣', '🂤', '🂥', '🂦', '🂧', '🂨', '🂩', '🂪', '🂫', '🂬', '🂭', '🂮']
   30     plane2_cjkpart1 = ['𠀀', '𠀁', '𠀂', '𠀃', '𠀄', '𠀅', '𠀆', '𠀇', '𠀈', '𠀉', '𠀊', '𠀋', '𠀌', '𠀍', '𠀎', '𠀏']
   31 
   32     testarrays = [numberchars; lowerchars; upperchars; plane1_playingcards; plane2_cjkpart1]
   33 
   34     #Integer(x::Char) = Int(x)
   35     #tests ASCII 48 - 57
   36     counter = 48
   37     for x in numberchars
   38         @test Integer(x) == counter
   39         counter += 1
   40     end
   41 
   42     #tests ASCII 65 - 90
   43     counter = 65
   44     for x in upperchars
   45         @test Integer(x) == counter
   46         counter += 1
   47     end
   48 
   49     #tests ASCII 97 - 122
   50     counter = 97
   51     for x in lowerchars
   52         @test Integer(x) == counter
   53         counter += 1
   54     end
   55 
   56     #tests Unicode plane 1: 127136 - 127150
   57     counter = 127136
   58     for x in plane1_playingcards
   59         @test Integer(x) == counter
   60         counter += 1
   61     end
   62 
   63     #tests Unicode plane 2: 131072 - 131087
   64     counter = 131072
   65     for x in plane2_cjkpart1
   66         @test Integer(x) == counter
   67         counter += 1
   68     end
   69 
   70     #convert(::Type{Char}, x::Float16) = char(convert(UInt32, x))
   71     #convert(::Type{Char}, x::Float32) = char(convert(UInt32, x))
   72     #convert(::Type{Char}, x::Float64) = char(convert(UInt32, x))
   73     for x = 1:9
   74         @test convert(Char, Float16(x)) == convert(Char, Float32(x)) == convert(Char, Float64(x)) == Char(x)
   75     end
   76 
   77     #size(c::Char) = ()
   78     for x in testarrays
   79         @test size(x) == ()
   80         @test_throws BoundsError size(x,0)
   81         @test size(x,1) == 1
   82     end
   83 
   84     #ndims(c::Char) = 0
   85     for x in testarrays
   86         @test ndims(x) == 0
   87     end
   88 
   89     #length(c::Char) = 1
   90     for x in testarrays
   91         @test length(x) == 1
   92     end
   93 
   94     #lastindex(c::Char) = 1
   95     for x in testarrays
   96         @test lastindex(x) == 1
   97     end
   98 
   99     #getindex(c::Char) = c
  100     for x in testarrays
  101         @test getindex(x) == x
  102     end
  103 
  104     #first(c::Char) = c
  105     for x in testarrays
  106         @test first(x) == x
  107     end
  108 
  109     #last(c::Char) = c
  110     for x in testarrays
  111         @test last(x) == x
  112     end
  113 
  114     #eltype(c::Char) = Char
  115     for x in testarrays
  116         @test eltype(x) == Char
  117     end
  118 
  119     #iterate(c::Char)
  120     for x in testarrays
  121         @test iterate(x)[1] == x
  122         @test iterate(x, iterate(x)[2]) == nothing
  123     end
  124 
  125     #isless(x::Char, y::Integer) = isless(UInt32(x), y)
  126     for x in upperchars
  127         @test isless(x, Char(91)) == true
  128     end
  129 
  130     for x in lowerchars
  131         @test isless(x, Char(123)) == true
  132     end
  133 
  134     for x in numberchars
  135         @test isless(x, Char(66)) == true
  136     end
  137 
  138     for x in plane1_playingcards
  139         @test isless(x, Char(127151)) == true
  140     end
  141 
  142     for x in plane2_cjkpart1
  143         @test isless(x, Char(131088)) == true
  144     end
  145 
  146     #isless(x::Integer, y::Char) = isless(x, UInt32(y))
  147     for x in upperchars
  148         @test isless(Char(64), x) == true
  149     end
  150 
  151     for x in lowerchars
  152         @test isless(Char(96), x) == true
  153     end
  154 
  155     for x in numberchars
  156         @test isless(Char(47), x) == true
  157     end
  158 
  159     for x in plane1_playingcards
  160         @test isless(Char(127135), x) == true
  161     end
  162 
  163     for x in plane2_cjkpart1
  164         @test isless(Char(131071), x) == true
  165     end
  166 
  167     @test !isequal('x', 120)
  168     @test convert(Signed, 'A') === Int32(65)
  169     @test convert(Unsigned, 'A') === UInt32(65)
  170 end #end of let block
  171 
  172 @testset "issue #14573" begin
  173     array = ['a', 'b', 'c'] + [1, 2, 3]
  174     @test array == ['b', 'd', 'f']
  175     @test eltype(array) == Char
  176 
  177     array = [1, 2, 3] + ['a', 'b', 'c']
  178     @test array == ['b', 'd', 'f']
  179     @test eltype(array) == Char
  180 
  181     array = ['a', 'b', 'c'] - [0, 1, 2]
  182     @test array == ['a', 'a', 'a']
  183     @test eltype(array) == Char
  184 end
  185 
  186 @testset "sprint, repr" begin
  187     @test sprint(show, "text/plain", '$') == "'\$': ASCII/Unicode U+0024 (category Sc: Symbol, currency)"
  188     @test repr('$') == "'\$'"
  189 end
  190 
  191 @testset "read incomplete character at end of stream or file" begin
  192     local file = tempname()
  193     local iob = IOBuffer([0xf0])
  194     local bytes(c::Char) = codeunits(string(c))
  195     @test bytes(read(iob, Char)) == [0xf0]
  196     @test eof(iob)
  197     try
  198         write(file, 0xf0)
  199         open(file) do io
  200             @test bytes(read(io, Char)) == [0xf0]
  201             @test eof(io)
  202         end
  203         let io = Base.Filesystem.open(file, Base.Filesystem.JL_O_RDONLY)
  204             @test bytes(read(io, Char)) == [0xf0]
  205             @test eof(io)
  206             close(io)
  207         end
  208     finally
  209         rm(file, force=true)
  210     end
  211 end
  212 
  213 @testset "overlong codes" begin
  214     function test_overlong(c::Char, n::Integer, rep::String)
  215         if isvalid(c)
  216             @test Int(c) == n
  217         else
  218             @test_throws Base.InvalidCharError UInt32(c)
  219         end
  220         @test sprint(show, c) == rep
  221         if Base.isoverlong(c)
  222             @test occursin(rep*": [overlong]", sprint(show, "text/plain", c))
  223         end
  224     end
  225 
  226     # TODO: use char syntax once #25072 is fixed
  227     test_overlong('\0', 0, "'\\0'")
  228     test_overlong("\xc0\x80"[1], 0, "'\\xc0\\x80'")
  229     test_overlong("\xe0\x80\x80"[1], 0, "'\\xe0\\x80\\x80'")
  230     test_overlong("\xf0\x80\x80\x80"[1], 0, "'\\xf0\\x80\\x80\\x80'")
  231 
  232     test_overlong('\x30', 0x30, "'0'")
  233     test_overlong("\xc0\xb0"[1], 0x30, "'\\xc0\\xb0'")
  234     test_overlong("\xe0\x80\xb0"[1], 0x30, "'\\xe0\\x80\\xb0'")
  235     test_overlong("\xf0\x80\x80\xb0"[1], 0x30, "'\\xf0\\x80\\x80\\xb0'")
  236 
  237     test_overlong('\u8430', 0x8430, "'萰'")
  238     test_overlong("\xf0\x88\x90\xb0"[1], 0x8430, "'\\xf0\\x88\\x90\\xb0'")
  239 end
  240 
  241 # create a new AbstractChar type to test the fallbacks
  242 primitive type ASCIIChar <: AbstractChar 8 end
  243 ASCIIChar(c::UInt8) = reinterpret(ASCIIChar, c)
  244 ASCIIChar(c::UInt32) = ASCIIChar(UInt8(c))
  245 Base.codepoint(c::ASCIIChar) = reinterpret(UInt8, c)
  246 
  247 @testset "abstractchar" begin
  248     @test AbstractChar('x') === AbstractChar(UInt32('x')) === 'x'
  249 
  250     @test isascii(ASCIIChar('x'))
  251     @test ASCIIChar('x') < 'y'
  252     @test ASCIIChar('x') == 'x' === Char(ASCIIChar('x')) === convert(Char, ASCIIChar('x'))
  253     @test ASCIIChar('x')^3 == "xxx"
  254     @test repr(ASCIIChar('x')) == "'x'"
  255     @test string(ASCIIChar('x')) == "x"
  256     @test_throws MethodError write(IOBuffer(), ASCIIChar('x'))
  257     @test_throws MethodError read(IOBuffer('x'), ASCIIChar)
  258 end
  259 
  260 @testset "ncodeunits(::Char)" begin
  261     # valid encodings
  262     @test ncodeunits('\0')       == 1
  263     @test ncodeunits('\x1')      == 1
  264     @test ncodeunits('\x7f')     == 1
  265     @test ncodeunits('\u80')     == 2
  266     @test ncodeunits('\uff')     == 2
  267     @test ncodeunits('\u7ff')    == 2
  268     @test ncodeunits('\u800')    == 3
  269     @test ncodeunits('\uffff')   == 3
  270     @test ncodeunits('\U10000')  == 4
  271     @test ncodeunits('\U10ffff') == 4
  272     # invalid encodings
  273     @test ncodeunits(reinterpret(Char, 0x80_00_00_00)) == 1
  274     @test ncodeunits(reinterpret(Char, 0x01_00_00_00)) == 1
  275     @test ncodeunits(reinterpret(Char, 0x00_80_00_00)) == 2
  276     @test ncodeunits(reinterpret(Char, 0x00_01_00_00)) == 2
  277     @test ncodeunits(reinterpret(Char, 0x00_00_80_00)) == 3
  278     @test ncodeunits(reinterpret(Char, 0x00_00_01_00)) == 3
  279     @test ncodeunits(reinterpret(Char, 0x00_00_00_80)) == 4
  280     @test ncodeunits(reinterpret(Char, 0x00_00_00_01)) == 4
  281 end
  282 
  283 @testset "reinterpret(Char, ::UInt32)" begin
  284     for s = 0:31
  285         u = one(UInt32) << s
  286         @test reinterpret(UInt32, reinterpret(Char, u)) === u
  287     end
  288 end
  289 
  290 @testset "broadcasting of Char" begin
  291     @test identity.('a') == 'a'
  292 end