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

Add tests for Base.cwstring #56123

Merged
merged 10 commits into from
Jan 8, 2025
Merged
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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Testset still fails (intentionally) on Windows, will my branch still be merged or should a fix addressing the tests be pushed first?

Copy link
Contributor

Choose a reason for hiding this comment

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

We aren't going to merge a PR with broken tests (unless explicitly marked so), but I'm not clear why we should just add a broken test. What's broken? The test? The code?

Copy link
Contributor Author

@rileysheridan rileysheridan Nov 16, 2024

Choose a reason for hiding this comment

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

Base.cwstring() converts an input string s to a NUL-terminated character vector but currently throws an error for any input string s that contains a NUL character anywhere. This is the problem test/input

str_2 = "Wordu\000"
# ...
@test Base.cwstring(str_2) == UInt16[0x0057, 0x006f, 0x0072, 0x0064, 0x0000]

where I input a string with a terminating NUL expecting cwstring() to trim the NUL off the end of the input and proceed to convert the remainder as normal, but instead cwstring() errors. Sorry for not clarifying earlier, but my ultimate question is should we add terminal NUL trimming to cwstring() or just leave the function alone? If the latter, I can just change my broken test to

@test_throws ArgumentError Base.cwstring(str_2)

so all tests in the testset work and we can merge this PR without issue.

# 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