Skip to content

Commit

Permalink
Tests: Add unit tests for the low-level iconv APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
rdw-software committed Jan 16, 2025
1 parent 7d0acf5 commit 8c13512
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Runtime/Bindings/FFI/iconv/iconv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ local ffi_string = ffi.string
local tonumber = tonumber
local tostring = tostring

local iconv = {}
local iconv = {
ERROR_CONVERSION_FAILED = ffi.cast("size_t", -1ULL),
}

iconv.cdefs = [[
typedef void* iconv_t;
Expand Down
52 changes: 52 additions & 0 deletions Tests/BDD/iconv-library.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,58 @@ describe("iconv", function()
assertEquals(numBytesWritten, 12)
end)
end)

describe("iconv_open", function()
local descriptors = {}
before(function()
descriptors.valid = iconv.bindings.iconv_open("CP949", "UTF-8")
descriptors.invalid = iconv.bindings.iconv_open("Not-a-real-encoding", "UTF-8")
assertEquals(table.count(descriptors), 2)
end)

after(function()
for label, descriptor in pairs(descriptors) do
iconv.bindings.iconv_close(descriptor) -- NOOP if invalid
end
end)

it("should indicate an error if the requested conversion isn't supported", function()
local descriptor = iconv.bindings.iconv_open("Not-a-real-encoding", "UTF-8")
assertEquals(ffi.cast("size_t", descriptor), iconv.ERROR_CONVERSION_FAILED)
iconv.bindings.iconv_close(descriptor)
end)

it("should return a valid handle if the conversion is supported", function()
local descriptor = iconv.bindings.iconv_open("CP949", "UTF-8")
assertFalse(ffi.cast("size_t", descriptor) == iconv.ERROR_CONVERSION_FAILED)
iconv.bindings.iconv_close(descriptor)
end)
end)

describe("iconv", function()
it("should be able to convert Windows encodings to UTF-8", function()
local descriptor = iconv.bindings.iconv_open("UTF-8", "CP949")
assert(descriptor ~= iconv.ERROR_CONVERSION_FAILED, "Failed to create conversion descriptor")

local input = "\192\175\192\250\192\206\197\205\198\228\192\204\189\186"
local inputSize = ffi.new("size_t[1]", #input)
local inputBuffer = ffi.new("char[?]", #input, input)
local inputRef = ffi.new("char*[1]", inputBuffer)

local outputSize = ffi.new("size_t[1]", 256)
local outputBuffer = ffi.new("char[256]")
local outputRef = ffi.new("char*[1]", outputBuffer)

local result = iconv.bindings.iconv(descriptor, inputRef, inputSize, outputRef, outputSize)
assert(result ~= iconv.ERROR_CONVERSION_FAILED, "Conversion failed")

local convertedSize = 256 - outputSize[0]
local converted = ffi.string(outputBuffer, convertedSize)
assertEquals(converted, "유저인터페이스")

iconv.bindings.iconv_close(descriptor)
end)
end)
end)

describe("convert", function()
Expand Down

0 comments on commit 8c13512

Please sign in to comment.