Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Tests for Base.cwstring #56123

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified contrib/asan/build.sh
100755 → 100644
Empty file.
Empty file modified contrib/asan/check.jl
100755 → 100644
Empty file.
Empty file modified contrib/bpftrace/gc_all.bt
100755 → 100644
Empty file.
Empty file modified contrib/bpftrace/gc_simple.bt
100755 → 100644
Empty file.
Empty file modified contrib/bpftrace/gc_stop_the_world_latency.bt
100755 → 100644
Empty file.
Empty file modified contrib/bpftrace/rt_all.bt
100755 → 100644
Empty file.
Empty file modified contrib/check-whitespace.jl
100755 → 100644
Empty file.
Empty file modified contrib/commit-name.sh
100755 → 100644
Empty file.
Empty file modified contrib/delete-all-rpaths.sh
100755 → 100644
Empty file.
Empty file modified contrib/download_cmake.sh
100755 → 100644
Empty file.
Empty file modified contrib/excise_stdlib.sh
100755 → 100644
Empty file.
Empty file modified contrib/fixup-libgfortran.sh
100755 → 100644
Empty file.
Empty file modified contrib/fixup-libstdc++.sh
100755 → 100644
Empty file.
Empty file modified contrib/fixup-rpath.sh
100755 → 100644
Empty file.
Empty file modified contrib/install.sh
100755 → 100644
Empty file.
Empty file modified contrib/julia-config.jl
100755 → 100644
Empty file.
Empty file modified contrib/mac/app/notarize_check.sh
100755 → 100644
Empty file.
Empty file modified contrib/mac/app/renotarize_dmg.sh
100755 → 100644
Empty file.
Empty file modified contrib/new-stdlib.sh
100755 → 100644
Empty file.
Empty file modified contrib/normalize_triplet.py
100755 → 100644
Empty file.
Empty file modified contrib/prepare_release.sh
100755 → 100644
Empty file.
Empty file modified contrib/relative_path.py
100755 → 100644
Empty file.
Empty file modified contrib/tsan/build.sh
100755 → 100644
Empty file.
Empty file modified contrib/windows/icon-readme.md
100755 → 100644
Empty file.
Empty file modified contrib/windows/julia-banner.bmp
100755 → 100644
Empty file.
Empty file modified contrib/windows/julia.ico
100755 → 100644
Empty file.
Empty file modified deps/tools/jlchecksum
100755 → 100644
Empty file.
Empty file modified deps/tools/jldownload
100755 → 100644
Empty file.
Empty file modified src/flisp/bootstrap.sh
100755 → 100644
Empty file.
15 changes: 15 additions & 0 deletions test/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1409,3 +1409,18 @@ end
@test transcode(String, transcode(UInt8, transcode(UInt16, str))) == str
end
end

@testset "cwstring" begin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Base.cwstring is only defined on Windows:

if ccall(:jl_get_UNAME, Any, ()) === :NT
"""
Base.cwstring(s)
Converts a string `s` to a NUL-terminated `Vector{Cwchar_t}`, suitable for passing to C
functions expecting a `Ptr{Cwchar_t}`. The main advantage of using this over the implicit
conversion provided by [`Cwstring`](@ref) is if the function is called multiple times with the
same argument.
This is only available on Windows.
"""
function cwstring(s::AbstractString)
bytes = codeunits(String(s))
0 in bytes && throw(ArgumentError("embedded NULs are not allowed in C strings: $(repr(s))"))
return push!(transcode(UInt16, bytes), 0)
end
end
This testset should only be run on Windows, otherwise it'll fail on other platforms, which means you should change this to

if Sys.iswindows()
    @testset "cwstring" begin
        # ...
    end
end

# empty string
str_0 = ""
# string with embedded NUL character
str_1 = "Au\000B"
# string with terminating NUL character
str_2 = "Wordu\000"
# "Regular" string with UTF-8 characters of differing byte counts
str_3 = "aܣ𒀀"
@test Base.cwstring(str_0) = UInt16[0x0000]
rileysheridan marked this conversation as resolved.
Show resolved Hide resolved
@test_throws ArgumentError Base.cwstring(str_1)
@test Base.cwstring(str_2) == UInt16[0x0057, 0x006f, 0x0072, 0x0064, 0x0000]
@test Base.cwstring(str_3) == UInt16[0x0061, 0x0723, 0xd808, 0xdc00, 0x0000]
end
Loading