1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import com.github.tschoonj.xraylib.Xraylib;
import com.github.tschoonj.xraylib.compoundData;
public class TestCompoundParser {
@ParameterizedTest(name="test_good_compounds {index} -> {arguments}")
@ValueSource(strings = {
"C19H29COOH",
"C12H10",
"C12H6O2",
"C6H5Br",
"C3H4OH(COOH)3",
"HOCH2CH2OH",
"C5H11NO2",
"CH3CH(CH3)CH3",
"NH2CH(C4H5N2)COOH",
"H2O",
"Ca5(PO4)3F",
"Ca5(PO4)3OH",
"Ca5.522(PO4.48)3OH",
"Ca5.522(PO.448)3OH"
})
public void test_good_compounds(String compound) {
compoundData cd = Xraylib.CompoundParser(compound);
}
@ParameterizedTest(name="test_bad_compounds {index} -> {arguments}")
@NullAndEmptySource
@ValueSource(strings = {
"CuI2ww",
"0C",
"2O",
"13Li",
"2(NO3)",
"H(2)",
"Ba(12)",
"Cr(5)3",
"Pb(13)2",
"Au(22)11",
"Au11(H3PO4)2)",
"Au11(H3PO4))2",
"Au(11(H3PO4))2",
"Ca5.522(PO.44.8)3OH",
"Ba[12]",
"Auu1",
"AuL1",
" ",
"\t",
"\n",
"Au L1",
"Au\tFe"
})
public void test_bad_compounds(String compound) {
assertThrows(IllegalArgumentException.class, () -> {
compoundData cd = Xraylib.CompoundParser(compound);
});
}
@Test
public void test_H2SO4() {
compoundData cd = Xraylib.CompoundParser("H2SO4");
assertEquals(cd.nElements, 3);
assertEquals(cd.molarMass, 98.09);
assertEquals(cd.nAtomsAll, 7.0);
assertArrayEquals(cd.Elements, new int[]{1, 8, 16});
assertArrayEquals(cd.massFractions, new double[]{0.02059333265368539, 0.6524620246712203, 0.32694464267509427}, 1E-8);
assertArrayEquals(cd.nAtoms, new double[]{2.0, 4.0, 1.0});
}
@Test
public void test_SymbolToAtomicNumber_Fe() {
assertEquals(Xraylib.SymbolToAtomicNumber("Fe"), 26);
}
@Test
public void test_SymbolToAtomicNumber_bad_symbol() {
IllegalArgumentException exc = assertThrows(IllegalArgumentException.class, () -> {
int Z = Xraylib.SymbolToAtomicNumber("Uu");
});
assertEquals(exc.getMessage(), "Invalid chemical symbol");
assertThrows(IllegalArgumentException.class, () -> {
int Z = Xraylib.SymbolToAtomicNumber(null);
});
assertEquals(exc.getMessage(), "Invalid chemical symbol");
}
@Test
public void test_AtomicNumberToSymbol_Fe() {
assertEquals(Xraylib.AtomicNumberToSymbol(26), "Fe");
}
@Test
public void test_AtomicNumberToSymbol_bad_symbol() {
IllegalArgumentException exc = assertThrows(IllegalArgumentException.class, () -> {
String symbol = Xraylib.AtomicNumberToSymbol(-2);
});
assertEquals(exc.getMessage(), Xraylib.Z_OUT_OF_RANGE);
exc = assertThrows(IllegalArgumentException.class, () -> {
String symbol = Xraylib.AtomicNumberToSymbol(108);
});
assertEquals(exc.getMessage(), Xraylib.Z_OUT_OF_RANGE);
}
static IntStream generateStreamOfZ() {
return IntStream.range(1, 108);
}
@ParameterizedTest(name="test_cross_validation {arguments}")
@MethodSource("generateStreamOfZ")
public void test_cross_validation(int Z) {
String symbol = Xraylib.AtomicNumberToSymbol(Z);
assertEquals(Xraylib.SymbolToAtomicNumber(symbol), Z);
}
}
|