mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-30 11:33:34 +08:00
SortedNotesFolder: Add another test
This commit is contained in:
@ -1,3 +1,5 @@
|
|||||||
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:fimber/fimber.dart';
|
import 'package:fimber/fimber.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:gitjournal/core/sorting_mode.dart';
|
import 'package:gitjournal/core/sorting_mode.dart';
|
||||||
@ -80,26 +82,49 @@ class SortedNotesFolder with NotesFolderNotifier implements NotesFolder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int _insertInCorrectPos(Note note) {
|
int _insertInCorrectPos(Note note) {
|
||||||
|
if (_notes.isEmpty) {
|
||||||
|
_notes.add(note);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
var i = _getInsertPos(note, 0, _notes.length - 1);
|
var i = _getInsertPos(note, 0, _notes.length - 1);
|
||||||
|
if (i == _notes.length) {
|
||||||
|
_notes.add(note);
|
||||||
|
} else {
|
||||||
_notes.insert(i, note);
|
_notes.insert(i, note);
|
||||||
|
}
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
int _getInsertPos(Note note, int low, int high) {
|
int _getInsertPos(Note note, int low, int high) {
|
||||||
int mid;
|
assert(low <= high);
|
||||||
|
|
||||||
|
int mid = high;
|
||||||
|
|
||||||
while (low <= high) {
|
while (low <= high) {
|
||||||
mid = low + ((high - low) ~/ 2);
|
mid = low + ((high - low) ~/ 2);
|
||||||
|
|
||||||
var r = _sortFunc(_notes[mid], note);
|
var r = _sortFunc(_notes[mid], note);
|
||||||
if (r == 0) {
|
if (r == 0) {
|
||||||
return mid;
|
return low;
|
||||||
} else if (r < 0) {
|
}
|
||||||
low = mid + 1;
|
|
||||||
|
if (low == high) {
|
||||||
|
if (r < 0) {
|
||||||
|
return low + 1;
|
||||||
} else {
|
} else {
|
||||||
high = mid - 1;
|
return low;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (r < 0) {
|
||||||
|
low = mid + 1;
|
||||||
|
} else {
|
||||||
|
high = max(low, mid - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(false);
|
||||||
return mid;
|
return mid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,5 +91,29 @@ void main() {
|
|||||||
expect(sf.notes[4].body, "1");
|
expect(sf.notes[4].body, "1");
|
||||||
expect(sf.notes[5].body, "0");
|
expect(sf.notes[5].body, "0");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('If still sorted while loading the notes', () async {
|
||||||
|
var folder = NotesFolderFS(null, tempDir.path);
|
||||||
|
var sf = SortedNotesFolder(
|
||||||
|
folder: folder,
|
||||||
|
sortingMode: SortingMode.Modified,
|
||||||
|
);
|
||||||
|
|
||||||
|
await folder.loadRecursively();
|
||||||
|
|
||||||
|
expect(sf.hasNotes, true);
|
||||||
|
expect(sf.isEmpty, false);
|
||||||
|
expect(sf.name.startsWith("__sorted_folder_test__"), true);
|
||||||
|
expect(sf.subFolders.length, 0);
|
||||||
|
expect(sf.notes.length, 5);
|
||||||
|
|
||||||
|
expect(sf.notes[0].body, "4");
|
||||||
|
expect(sf.notes[1].body, "3");
|
||||||
|
expect(sf.notes[2].body, "2");
|
||||||
|
expect(sf.notes[3].body, "1");
|
||||||
|
expect(sf.notes[4].body, "0");
|
||||||
|
});
|
||||||
|
|
||||||
|
// FIXME: Test if adding a note to the end works
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user