Skip to content

Commit dd00453

Browse files
sabiwarajosevalim
authored andcommitted
Fixes to support 0TP27 (#13351)
* Fix non-deterministic key-value tests * Fix non-deterministic Enum tests * Fix :only option when deriving Inspect * Float.ceil and Float.floor return -0.0 for negative numbers * Fix non-deterministic Registry doctest * Simplify check
1 parent 2512cba commit dd00453

File tree

7 files changed

+48
-20
lines changed

7 files changed

+48
-20
lines changed

lib/elixir/lib/enum.ex

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3793,9 +3793,6 @@ defmodule Enum do
37933793
iex> Enum.unzip([{:a, 1}, {:b, 2}, {:c, 3}])
37943794
{[:a, :b, :c], [1, 2, 3]}
37953795
3796-
iex> Enum.unzip(%{a: 1, b: 2})
3797-
{[:a, :b], [1, 2]}
3798-
37993796
"""
38003797
@spec unzip(t) :: {[element], [element]}
38013798

@@ -4051,7 +4048,7 @@ defmodule Enum do
40514048
...> end)
40524049
[{1, 2, 3}, {1, 2, 3}]
40534050
4054-
iex> enums = [[1, 2], %{a: 3, b: 4}, [5, 6]]
4051+
iex> enums = [[1, 2], [a: 3, b: 4], [5, 6]]
40554052
...> Enum.zip_reduce(enums, [], fn elements, acc ->
40564053
...> [List.to_tuple(elements) | acc]
40574054
...> end)

lib/elixir/lib/float.ex

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ defmodule Float do
275275
-56.0
276276
iex> Float.ceil(34.251, 2)
277277
34.26
278+
iex> Float.ceil(-0.01)
279+
-0.0
278280
279281
"""
280282
@spec ceil(float, precision_range) :: float
@@ -332,6 +334,8 @@ defmodule Float do
332334
-6.0
333335
iex> Float.round(12.341444444444441, 15)
334336
12.341444444444441
337+
iex> Float.round(-0.01)
338+
-0.0
335339
336340
"""
337341
@spec round(float, precision_range) :: float
@@ -340,8 +344,13 @@ defmodule Float do
340344
# and could be implemented in the future.
341345
def round(float, precision \\ 0)
342346

347+
def round(float, 0) when float == 0.0, do: float
348+
343349
def round(float, 0) when is_float(float) do
344-
float |> :erlang.round() |> :erlang.float()
350+
case float |> :erlang.round() |> :erlang.float() do
351+
zero when zero == 0.0 and float < 0.0 -> -0.0
352+
rounded -> rounded
353+
end
345354
end
346355

347356
def round(float, precision) when is_float(float) and precision in @precision_range do
@@ -365,6 +374,8 @@ defmodule Float do
365374
case rounding do
366375
:ceil when sign === 0 -> 1 / power_of_10(precision)
367376
:floor when sign === 1 -> -1 / power_of_10(precision)
377+
:ceil when sign === 1 -> -0.0
378+
:half_up when sign === 1 -> -0.0
368379
_ -> 0.0
369380
end
370381

@@ -394,6 +405,9 @@ defmodule Float do
394405
boundary = den <<< 52
395406

396407
cond do
408+
num == 0 and sign == 1 ->
409+
-0.0
410+
397411
num == 0 ->
398412
0.0
399413

lib/elixir/lib/inspect.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,8 @@ end
535535

536536
defimpl Inspect, for: Any do
537537
defmacro __deriving__(module, struct, options) do
538-
fields = Map.keys(struct) -- [:__exception__, :__struct__]
538+
fields = Enum.sort(Map.keys(struct) -- [:__exception__, :__struct__])
539+
539540
only = Keyword.get(options, :only, fields)
540541
except = Keyword.get(options, :except, [])
541542
optional = Keyword.get(options, :optional, [])
@@ -545,7 +546,7 @@ defimpl Inspect, for: Any do
545546
:ok = validate_option(:optional, optional, fields, module)
546547

547548
inspect_module =
548-
if fields == only and except == [] do
549+
if fields == Enum.sort(only) and except == [] do
549550
Inspect.Map
550551
else
551552
Inspect.Any

lib/elixir/lib/map.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ defmodule Map do
147147
148148
## Examples
149149
150-
iex> Map.keys(%{a: 1, b: 2})
150+
Map.keys(%{a: 1, b: 2})
151151
[:a, :b]
152152
153153
"""
@@ -161,7 +161,7 @@ defmodule Map do
161161
162162
## Examples
163163
164-
iex> Map.values(%{a: 1, b: 2})
164+
Map.values(%{a: 1, b: 2})
165165
[1, 2]
166166
167167
"""

lib/elixir/lib/registry.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,16 +1301,16 @@ defmodule Registry do
13011301
iex> Registry.start_link(keys: :unique, name: Registry.SelectAllTest)
13021302
iex> {:ok, _} = Registry.register(Registry.SelectAllTest, "hello", :value)
13031303
iex> {:ok, _} = Registry.register(Registry.SelectAllTest, "world", :value)
1304-
iex> Registry.select(Registry.SelectAllTest, [{{:"$1", :"$2", :"$3"}, [], [{{:"$1", :"$2", :"$3"}}]}])
1305-
[{"world", self(), :value}, {"hello", self(), :value}]
1304+
iex> Registry.select(Registry.SelectAllTest, [{{:"$1", :"$2", :"$3"}, [], [{{:"$1", :"$2", :"$3"}}]}]) |> Enum.sort()
1305+
[{"hello", self(), :value}, {"world", self(), :value}]
13061306
13071307
Get all keys in the registry:
13081308
13091309
iex> Registry.start_link(keys: :unique, name: Registry.SelectAllTest)
13101310
iex> {:ok, _} = Registry.register(Registry.SelectAllTest, "hello", :value)
13111311
iex> {:ok, _} = Registry.register(Registry.SelectAllTest, "world", :value)
1312-
iex> Registry.select(Registry.SelectAllTest, [{{:"$1", :_, :_}, [], [:"$1"]}])
1313-
["world", "hello"]
1312+
iex> Registry.select(Registry.SelectAllTest, [{{:"$1", :_, :_}, [], [:"$1"]}]) |> Enum.sort()
1313+
["hello", "world"]
13141314
13151315
"""
13161316
@doc since: "1.9.0"

lib/elixir/test/elixir/enum_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ defmodule EnumTest do
5656
end
5757

5858
test "mix and match" do
59-
enums = [[1, 2], %{a: 3, b: 4}, [5, 6]]
59+
enums = [[1, 2], 3..4, [5, 6]]
6060
result = Enum.zip_reduce(enums, [], fn elements, acc -> [List.to_tuple(elements) | acc] end)
61-
assert result == [{2, {:b, 4}, 6}, {1, {:a, 3}, 5}]
61+
assert result == [{2, 4, 6}, {1, 3, 5}]
6262
end
6363
end
6464

@@ -412,7 +412,7 @@ defmodule EnumTest do
412412
assert Enum.into([a: 1, b: 2], %{c: 3}) == %{a: 1, b: 2, c: 3}
413413
assert Enum.into(MapSet.new(a: 1, b: 2), %{}) == %{a: 1, b: 2}
414414
assert Enum.into(MapSet.new(a: 1, b: 2), %{c: 3}) == %{a: 1, b: 2, c: 3}
415-
assert Enum.into(%{a: 1, b: 2}, []) == [a: 1, b: 2]
415+
assert Enum.into(%{a: 1, b: 2}, []) |> Enum.sort() == [a: 1, b: 2]
416416
assert Enum.into(1..3, []) == [1, 2, 3]
417417
assert Enum.into(["H", "i"], "") == "Hi"
418418
end
@@ -1430,7 +1430,7 @@ defmodule EnumTest do
14301430
test "unzip/1" do
14311431
assert Enum.unzip([{:a, 1}, {:b, 2}, {:c, 3}]) == {[:a, :b, :c], [1, 2, 3]}
14321432
assert Enum.unzip([]) == {[], []}
1433-
assert Enum.unzip(%{a: 1, b: 2}) == {[:a, :b], [1, 2]}
1433+
assert Enum.unzip(%{a: 1}) == {[:a], [1]}
14341434
assert Enum.unzip(foo: "a", bar: "b") == {[:foo, :bar], ["a", "b"]}
14351435

14361436
assert_raise FunctionClauseError, fn -> Enum.unzip([{:a, 1}, {:b, 2, "foo"}]) end

lib/elixir/test/elixir/float_test.exs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ defmodule FloatTest do
104104
assert Float.ceil(7.5432e3) === 7544.0
105105
assert Float.ceil(7.5e-3) === 1.0
106106
assert Float.ceil(-12.32453e4) === -123_245.0
107-
assert Float.ceil(-12.32453e-10) === 0.0
107+
assert Float.ceil(-12.32453e-10) === -0.0
108108
assert Float.ceil(0.32453e-10) === 1.0
109-
assert Float.ceil(-0.32453e-10) === 0.0
109+
assert Float.ceil(-0.32453e-10) === -0.0
110110
assert Float.ceil(1.32453e-10) === 1.0
111111
assert Float.ceil(0.0) === 0.0
112112
end
@@ -130,7 +130,7 @@ defmodule FloatTest do
130130
assert Float.ceil(-12.524235, 3) === -12.524
131131

132132
assert Float.ceil(12.32453e-20, 2) === 0.01
133-
assert Float.ceil(-12.32453e-20, 2) === 0.0
133+
assert Float.ceil(-12.32453e-20, 2) === -0.0
134134

135135
assert Float.ceil(0.0, 2) === 0.0
136136

@@ -139,6 +139,11 @@ defmodule FloatTest do
139139
end
140140
end
141141

142+
test "with small floats rounded up to -0.0" do
143+
assert Float.ceil(-0.1, 0) === -0.0
144+
assert Float.ceil(-0.01, 1) === -0.0
145+
end
146+
142147
test "with subnormal floats" do
143148
assert Float.ceil(5.0e-324, 0) === 1.0
144149
assert Float.ceil(5.0e-324, 1) === 0.1
@@ -172,6 +177,17 @@ defmodule FloatTest do
172177
end
173178
end
174179

180+
test "with small floats rounded to +0.0 / -0.0" do
181+
assert Float.round(0.01, 0) === 0.0
182+
assert Float.round(0.01, 1) === 0.0
183+
184+
assert Float.round(-0.01, 0) === -0.0
185+
assert Float.round(-0.01, 1) === -0.0
186+
187+
assert Float.round(-0.49999, 0) === -0.0
188+
assert Float.round(-0.049999, 1) === -0.0
189+
end
190+
175191
test "with subnormal floats" do
176192
for precision <- 0..15 do
177193
assert Float.round(5.0e-324, precision) === 0.0

0 commit comments

Comments
 (0)