SortedNotesFolder: Add another test

This commit is contained in:
Vishesh Handa
2020-03-20 23:47:56 +01:00
parent c5bcb09ec0
commit 03ff93bfc9
2 changed files with 54 additions and 5 deletions

View File

@ -1,3 +1,5 @@
import 'dart:math';
import 'package:fimber/fimber.dart';
import 'package:flutter/material.dart';
import 'package:gitjournal/core/sorting_mode.dart';
@ -80,26 +82,49 @@ class SortedNotesFolder with NotesFolderNotifier implements NotesFolder {
}
int _insertInCorrectPos(Note note) {
if (_notes.isEmpty) {
_notes.add(note);
return 0;
}
var i = _getInsertPos(note, 0, _notes.length - 1);
_notes.insert(i, note);
if (i == _notes.length) {
_notes.add(note);
} else {
_notes.insert(i, note);
}
return i;
}
int _getInsertPos(Note note, int low, int high) {
int mid;
assert(low <= high);
int mid = high;
while (low <= high) {
mid = low + ((high - low) ~/ 2);
var r = _sortFunc(_notes[mid], note);
if (r == 0) {
return mid;
} else if (r < 0) {
return low;
}
if (low == high) {
if (r < 0) {
return low + 1;
} else {
return low;
}
}
if (r < 0) {
low = mid + 1;
} else {
high = mid - 1;
high = max(low, mid - 1);
}
}
assert(false);
return mid;
}

View File

@ -91,5 +91,29 @@ void main() {
expect(sf.notes[4].body, "1");
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
});
}