package comp1110.exam;
import comp1110.exam.Q1CropRotation.Vegetable;
import comp1110.exam.Q1CropRotation.Group;
Copyright By PowCoder代写 加微信 powcoder
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.runners.MethodSorters;
import java.util.*;
import static org.junit.Assert.*;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class Q1CropRotationTest {
public Timeout globalTimeout = Timeout.millis(1000);
final static Vegetable BOK_CHOY = new Vegetable(” “, Group.BRASSICA);
final static Vegetable CAPSICUM = new Vegetable(“Capsicum”, Group.FRUITING);
final static Vegetable EGGPLANT = new Vegetable(“Eggplant”, Group.FRUITING);
final static Vegetable GAI_LAN = new Vegetable(” “, Group.BRASSICA);
final static Vegetable KALE = new Vegetable(“Kale”, Group.BRASSICA);
final static Vegetable LEEK = new Vegetable(“Leek”, Group.ALLIUM);
final static Vegetable ONION = new Vegetable(“Onion”, Group.ALLIUM);
final static Vegetable PEA = new Vegetable(“Pea”, Group.LEGUME);
final static Vegetable RUNNER_BEAN = new Vegetable(“Runner Bean”, Group.LEGUME);
final static Vegetable TOMATO = new Vegetable(“Tomato”, Group.FRUITING);
public void testTrivial() {
checkAllRotations(“”, 1); // no veges
checkAllRotations(“L”, 0); // zero seasons
checkAllRotations(“L”, 1, “L”); // one crop, one season
checkAllRotations(“L”, 2); // one crop, multiple seasons
public void testTwoSeasons() {
checkAllRotations(“TG”, 2); // no valid succession order
checkAllRotations(“OB”, 2, “BO”);
checkAllRotations(“KR”, 2, “RK”);
checkAllRotations(“RK”, 2, “RK”);
checkAllRotations(“GR”, 2, “RG”);
checkAllRotations(“TP”, 2, “TP”);
public void testFourSeasons() {
checkAllRotations(“GCRK”, 4); // only three groups
checkAllRotations(“KOTP”, 4, shuffleComplete(4, new String[]{“KOTP”}));
checkAllRotations(“LBCP”, 4, shuffleComplete(4, new String[]{“PBLC”}));
checkAllRotations(“GCRO”, 4, shuffleComplete(4, new String[]{“RGOC”}));
public void testFiveSeasons() {
checkAllRotations(“CKOTE”, 5); // only three groups
checkAllRotations(“KOTPR”, 5, “RKOTP”, “PKOTR”);
checkAllRotations(“ECRGL”, 5, “CRGLE”, “ERGLC”);
checkAllRotations(“OCRGL”, 5, “OCRGL”, “LCRGO”);
checkAllRotations(“ROTGP”, 5, “RGOTP”, “PGOTR”);
public void testEightSeasons() {
checkAllRotations(“EKLCPRGO”, 8, shuffleComplete(8, new String[]{“CRKOEPGL”, “CRGOEPKL”, “EPGOCRKL”, “CPGOERKL”, “CPGLERKO”, “EPKLCRGO”, “EPKOCRGL”, “CPKLERGO”, “CPKOERGL”}));
checkAllRotations(“LCRPTGOB”, 8, shuffleComplete(8, new String[]{“CRBOTPGL”, “CRGOTPBL”, “TPGOCRBL”, “CPGOTRBL”, “CPGLTRBO”, “TPBLCRGO”, “TPBOCRGL”, “CPBLTRGO”, “CPBOTRGL”}));
private void checkAllRotations(String vegeString, int seasons, String… expectedRotations) {
Set
veges.addAll(vegFromString(vegeString));
Set> expected = rotationFromStrings(expectedRotations);
Set> result = Q1CropRotation.getAllRotations(veges, seasons);
String expectedString;
if (expected.isEmpty()) {
expectedString = “empty set”;
StringBuilder sb = new StringBuilder();
for (List
sb.append(“- “).append(r).append(“\n”);
expectedString = sb.toString();
assertNotNull(“A null value was returned from Q1CropRotation.getAllRotations(” + veges + “, ” + seasons + “)\nexpected:\n” + expectedString, result);
String resultString;
if (result.isEmpty()) {
resultString = “empty set”;
StringBuilder sb2 = new StringBuilder();
for (List
sb2.append(“- “).append(v).append(“\n”);
resultString = sb2.toString();
nextExpected:
for (List
for (List
if (e.equals(r)) {
continue nextExpected;
fail(“An expected valid rotation ” + e + ” \nwas not returned from Q1CropRotation.getAllRotations(” + veges + “, ” + seasons + “)\nexpected:\n” + expectedString + “but got:\n” + resultString);
nextResult:
for (List
for (List
if (e.equals(r)) {
continue nextResult;
fail(“Unexpected rotation ” + r + “\nwas returned from Q1CropRotation.getAllRotations(” + veges + “, ” + seasons + “)\nexpected:\n” + expectedString);
private List
List
int i = 0;
for (char c : v.toCharArray()) {
switch (c) {
result.add(BOK_CHOY);
result.add(CAPSICUM);
result.add(EGGPLANT);
result.add(GAI_LAN);
result.add(KALE);
result.add(LEEK);
result.add(ONION);
result.add(PEA);
result.add(RUNNER_BEAN);
result.add(TOMATO);
throw new IllegalArgumentException(“Unknown vegetable character: ” + c);
return result;
private Set> rotationFromStrings(String… rotations) {
Set> result = new HashSet<>();
int i = 0;
for (String s : rotations) {
result.add(vegFromString(s));
return result;
private String[] shuffleComplete(int seasons, String[] source) {
String[] result = new String[source.length * seasons];
int i = 0;
for (String s : source) {
for (int j = 0; j < s.length(); j++) {
result[i++] = s.substring(j) + s.substring(0, j);
return result;
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com