diff --git a/src/automate.c b/src/automate.c index c1d4b384b..f195bb6db 100644 --- a/src/automate.c +++ b/src/automate.c @@ -40,8 +40,12 @@ int autostudy_init(student students[], int max_students, region *r) init_order(u->thisorder, u->faction->locale); st->sk = getskill(u->faction->locale); st->level = effskill_study(u, st->sk); + st->learn = 0; } } + if (nstudents > 0) { + qsort(students, nstudents, sizeof(student), cmp_students); + } return nstudents; } @@ -55,7 +59,6 @@ void do_autostudy(region *r) { int i; skill_t sk = NOSKILL; - qsort(students, nstudents, sizeof(student), cmp_students); for (i = 0; i != nstudents; ++i) { if (students[i].u) { if (sk == NOSKILL) { diff --git a/src/automate.test.c b/src/automate.test.c index d8dc8ed74..3ea640cdf 100644 --- a/src/automate.test.c +++ b/src/automate.test.c @@ -15,7 +15,7 @@ static void test_autostudy(CuTest *tc) { student students[4]; - unit *u1, *u2; + unit *u1, *u2, *u3; faction *f; region *r; @@ -24,9 +24,27 @@ static void test_autostudy(CuTest *tc) { f = test_create_faction(NULL); u1 = test_create_unit(f, r); u1->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_ENTERTAINMENT]); + test_create_unit(f, r); u2 = test_create_unit(f, r); u2->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_ENTERTAINMENT]); - CuAssertIntEquals(tc, 2, autostudy_init(students, 4, r)); + set_level(u2, SK_ENTERTAINMENT, 2); + u3 = test_create_unit(f, r); + u3->thisorder = create_order(K_AUTOSTUDY, f->locale, skillnames[SK_PERCEPTION]); + students[3].u = NULL; + CuAssertIntEquals(tc, 3, autostudy_init(students, 4, r)); + CuAssertPtrEquals(tc, u2, students[0].u); + CuAssertIntEquals(tc, 2, students[0].level); + CuAssertIntEquals(tc, 0, students[0].learn); + CuAssertIntEquals(tc, SK_ENTERTAINMENT, students[0].sk); + CuAssertPtrEquals(tc, u1, students[1].u); + CuAssertIntEquals(tc, 0, students[1].level); + CuAssertIntEquals(tc, 0, students[1].learn); + CuAssertIntEquals(tc, SK_ENTERTAINMENT, students[1].sk); + CuAssertPtrEquals(tc, u3, students[2].u); + CuAssertIntEquals(tc, 0, students[2].level); + CuAssertIntEquals(tc, 0, students[2].learn); + CuAssertIntEquals(tc, SK_PERCEPTION, students[2].sk); + CuAssertPtrEquals(tc, NULL, students[3].u); test_teardown(); }