Skip to content

Commit

Permalink
Fix bug with Sequence.sorted()
Browse files Browse the repository at this point in the history
  • Loading branch information
GrieferAtWork committed May 18, 2024
1 parent 9818bb9 commit e501c4a
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 8 deletions.
52 changes: 44 additions & 8 deletions src/deemon/objects/seq.c
Original file line number Diff line number Diff line change
Expand Up @@ -4043,12 +4043,29 @@ seq_reversed(DeeObject *self, size_t argc, DeeObject *const *argv, DeeObject *kw
DREF DeeObject *result;
size_t start = 0, end = (size_t)-1;
if (DeeArg_UnpackKw(argc, argv, kw, kwlist__start_end,
"|" UNPuSIZ UNPuSIZ ":reverse",
"|" UNPuSIZ UNPuSIZ ":reversed",
&start, &end))
goto err;
result = DeeList_FromSequence(self);
if likely(result)
DeeList_Reverse(result, start, end);
if unlikely(!result)
goto err;
if (end > DeeList_SIZE((DeeListObject *)result))
end = DeeList_SIZE((DeeListObject *)result);
if (start > end)
start = end;
if (start > 0 || end < DeeList_SIZE((DeeListObject *)result)) {
/* Super hacky, but OK since this code will be removed anyways */
DREF DeeObject *result2;
result2 = DeeObject_GetRangeIndex(result, (Dee_ssize_t)start, (Dee_ssize_t)end);
Dee_Decref(result);
if unlikely(!result2)
goto err;
result = DeeList_FromSequence(result2);
Dee_Decref(result2);
if unlikely(!result)
goto err;
}
DeeList_Reverse(result, 0, (size_t)-1);
return result;
err:
return NULL;
Expand All @@ -4058,17 +4075,36 @@ PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL
seq_sorted(DeeObject *self, size_t argc,
DeeObject *const *argv, DeeObject *kw) {
DREF DeeObject *result;
DeeObject *key = NULL;
if (DeeArg_UnpackKw(argc, argv, kw, kwlist__key, "|o:sorted", &key))
size_t start = 0, end = (size_t)-1;
DeeObject *key = Dee_None;
if (DeeArg_UnpackKw(argc, argv, kw, kwlist__start_end_key,
"|" UNPuSIZ UNPuSIZ "o:sorted",
&start, &end, &key))
goto err;
if (DeeNone_Check(key))
key = NULL;
result = DeeList_FromSequence(self);
if unlikely(!result)
goto err;
if (end > DeeList_SIZE((DeeListObject *)result))
end = DeeList_SIZE((DeeListObject *)result);
if (start > end)
start = end;
if (start > 0 || end < DeeList_SIZE((DeeListObject *)result)) {
/* Super hacky, but OK since this code will be removed anyways */
DREF DeeObject *result2;
result2 = DeeObject_GetRangeIndex(result, (Dee_ssize_t)start, (Dee_ssize_t)end);
Dee_Decref(result);
if unlikely(!result2)
goto err;
result = DeeList_FromSequence(result2);
Dee_Decref(result2);
if unlikely(!result)
goto err;
}
if unlikely(DeeList_Sort(result, 0, (size_t)-1, key))
Dee_Clear(result);
goto err_r;
return result;
err_r:
Dee_Decref(result);
err:
return NULL;
}
Expand Down
34 changes: 34 additions & 0 deletions util/test/deemon-sequence-reversed.dee
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/deemon
/* Copyright (c) 2018-2024 Griefer@Work *
* *
* This software is provided 'as-is', without any express or implied *
* warranty. In no event will the authors be held liable for any damages *
* arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not *
* claim that you wrote the original software. If you use this software *
* in a product, an acknowledgement (see the following) in the product *
* documentation is required: *
* Portions Copyright (c) 2018-2024 Griefer@Work *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* 3. This notice may not be removed or altered from any source distribution. *
*/

import * from deemon;

local BASESEQ = (1, 5, 3, 2, 7);

for (local seq: {
BASESEQ,
BASESEQ as Sequence,
List(BASESEQ),
List(BASESEQ) as Sequence,
}) {
assert seq.reversed() == {7,2,3,5,1};
assert seq.reversed(2, 5) == {7,2,3};
}
34 changes: 34 additions & 0 deletions util/test/deemon-sequence-sorted.dee
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/deemon
/* Copyright (c) 2018-2024 Griefer@Work *
* *
* This software is provided 'as-is', without any express or implied *
* warranty. In no event will the authors be held liable for any damages *
* arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not *
* claim that you wrote the original software. If you use this software *
* in a product, an acknowledgement (see the following) in the product *
* documentation is required: *
* Portions Copyright (c) 2018-2024 Griefer@Work *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* 3. This notice may not be removed or altered from any source distribution. *
*/

import * from deemon;

local BASESEQ = (1, 5, 3, 2, 7);

for (local seq: {
BASESEQ,
BASESEQ as Sequence,
List(BASESEQ),
List(BASESEQ) as Sequence,
}) {
assert seq.sorted() == {1,2,3,5,7};
assert seq.sorted(2, 5) == {2,3,7};
}

0 comments on commit e501c4a

Please sign in to comment.