Skip to content

Commit e46f288

Browse files
committed
Don't attempt to sort empty or singleton lists, also fix missed internal variable name changes
1 parent d8e294f commit e46f288

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

gm-stream/objects/obj_tests/Create_0.gml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,36 @@ function testSort() {
376376
return new TestResult(testName, true, "");
377377
}
378378

379+
function testSortEmptyList() {
380+
var testName = "Sort Empty List";
381+
var initialArray = [];
382+
383+
var result = stream_of(initialArray)
384+
.sort()
385+
.collectJoining();
386+
387+
if (result != "") {
388+
return new TestResult(testName, false, "expected result to be empty");
389+
}
390+
391+
return new TestResult(testName, true, "");
392+
}
393+
394+
function testSortMonoList() {
395+
var testName = "Sort Mono List";
396+
var initialArray = ["A"];
397+
398+
var result = stream_of(initialArray)
399+
.sort()
400+
.collectJoining();
401+
402+
if (result != "A") {
403+
return new TestResult(testName, false, "expected result to be same as input");
404+
}
405+
406+
return new TestResult(testName, true, "");
407+
}
408+
379409
function testSortNumeric() {
380410
var testName = "Sort Simple (Numeric)";
381411
var initialArray = [3, 1, 20];
@@ -498,7 +528,7 @@ totalCount = 0;
498528
failedCount = 0;
499529
testSuite = [testListToStream, testArrayToStream, testCollectList, testCollectArray, testDistinct, testCount, testFilter, testMap, testAllMatchTrue, testAllMatchFalse,
500530
testAnyMatchTrue, testAnyMatchFalse, testForEach, testCollectJoining, testCollectJoiningDelimiter, testCollectJoiningDelimiterPrefix,
501-
testCollectJoiningDelimiterPrefixSuffix, testFindFirstEmpty, testFindFirst, testSort, testSortNumeric, testSortComparator, testNoneMatchTrue,
531+
testCollectJoiningDelimiterPrefixSuffix, testFindFirstEmpty, testFindFirst, testSort, testSortEmptyList, testSortMonoList, testSortNumeric, testSortComparator, testNoneMatchTrue,
502532
testNoneMatchFalse, testQueueToStream, testStackToStream, testFold, testReduce, testReduceEmpty];
503533
show_debug_message("\nBeginning test suite\n");
504534
for (var i = 0; i < array_length(testSuite); i++) {

gm-stream/scripts/scr_gmstream/scr_gmstream.gml

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@ function GmStream(_gms_data) constructor {
4646
}
4747

4848
sort = function(comparatorFunction) {
49-
if (is_undefined(comparatorFunction) && isSorted == false) {
50-
ds_list_sort(gms_data, true);
51-
isSorted = true;
52-
} else {
53-
// Don't check or set isSorted in this case as a new comparator function could re-sort us
54-
var result = self.mergesort(gms_data, comparatorFunction);
55-
ds_list_copy(gms_data, result);
56-
ds_list_destroy(result);
49+
if (ds_list_size(gms_data) > 1) {
50+
if (is_undefined(comparatorFunction) && isSorted == false) {
51+
ds_list_sort(gms_data, true);
52+
isSorted = true;
53+
} else {
54+
// Don't check or set isSorted in this case as a new comparator function could re-sort us
55+
var result = self.mergesort(gms_data, comparatorFunction);
56+
ds_list_copy(gms_data, result);
57+
ds_list_destroy(result);
58+
}
5759
}
5860
return self;
5961
}
@@ -144,8 +146,8 @@ function GmStream(_gms_data) constructor {
144146

145147
fold = function(initialValue, foldFunction) {
146148
var result = initialValue;
147-
for (var i = 0; i < ds_list_size(data); i++) {
148-
result = foldFunction(result, data[| i]);
149+
for (var i = 0; i < ds_list_size(gms_data); i++) {
150+
result = foldFunction(result, gms_data[| i]);
149151
}
150152
self.clean_up();
151153
return result;
@@ -164,13 +166,13 @@ function GmStream(_gms_data) constructor {
164166
}
165167

166168
reduce = function(foldFunction) {
167-
if (ds_list_size(data) == 0) {
169+
if (ds_list_size(gms_data) == 0) {
168170
self.clean_up();
169171
throw "Cannot reduce an empty stream";
170172
}
171-
var result = data[| 0];
172-
for (var i = 1; i < ds_list_size(data); i++) {
173-
result = foldFunction(result, data[| i]);
173+
var result = gms_data[| 0];
174+
for (var i = 1; i < ds_list_size(gms_data); i++) {
175+
result = foldFunction(result, gms_data[| i]);
174176
}
175177
self.clean_up();
176178
return result;

0 commit comments

Comments
 (0)