[78] | 1 | package com.framsticks.parsers; |
---|
| 2 | |
---|
[84] | 3 | import org.testng.annotations.*; |
---|
[87] | 4 | |
---|
[78] | 5 | import com.framsticks.model.*; |
---|
[88] | 6 | import com.framsticks.model.f0.Schema; |
---|
[90] | 7 | import com.framsticks.model.f0.SchemaBuilder; |
---|
[78] | 8 | import com.framsticks.params.*; |
---|
| 9 | import com.framsticks.params.types.FloatParam; |
---|
[87] | 10 | import com.framsticks.params.types.StringParam; |
---|
[84] | 11 | import com.framsticks.test.TestConfiguration; |
---|
| 12 | import com.framsticks.util.math.Point3d; |
---|
[78] | 13 | |
---|
[84] | 14 | import java.io.IOException; |
---|
| 15 | import java.text.ParseException; |
---|
[78] | 16 | import java.util.List; |
---|
[84] | 17 | import static org.fest.assertions.Assertions.*; |
---|
| 18 | import static org.fest.assertions.Delta.*; |
---|
[78] | 19 | |
---|
| 20 | /** |
---|
| 21 | * Author: Piotr Śniegowski |
---|
| 22 | */ |
---|
[84] | 23 | public class F0ParserTest extends TestConfiguration { |
---|
[78] | 24 | |
---|
[84] | 25 | private Schema schema; |
---|
| 26 | private List<AccessInterface> accesses; |
---|
[90] | 27 | private List<ModelComponent> components; |
---|
[84] | 28 | private Model model; |
---|
| 29 | |
---|
| 30 | @BeforeClass |
---|
| 31 | public void setUp() throws Exception { |
---|
[90] | 32 | schema = new SchemaBuilder().stream(Schema.getDefaultDefinitionAsStream()).finish(); |
---|
[84] | 33 | } |
---|
| 34 | |
---|
[78] | 35 | @Test |
---|
[90] | 36 | public void checkFramsClasses() { |
---|
| 37 | FramsClass modelClass = schema.getRegistry().getFramsClass("Model"); |
---|
| 38 | assertThat(modelClass).isNotNull(); |
---|
| 39 | assertThat(modelClass.getParam("se")).isInstanceOf(FloatParam.class); |
---|
| 40 | // assertThat(modelClass.getParam("energ0")).isInstanceOf(FloatParam.class); |
---|
| 41 | assertThat(modelClass.getParam("Vstyle")).isInstanceOf(StringParam.class); |
---|
| 42 | assertThat(modelClass.getParamCount()).isEqualTo(6); |
---|
| 43 | // assertThat(); |
---|
| 44 | |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | @Test(dependsOnMethods = "checkFramsClasses") |
---|
[84] | 48 | public void primitiveParam() { |
---|
[88] | 49 | FramsClass joint = schema.getFramsClass("j"); |
---|
[87] | 50 | PrimitiveParam<?> dx = joint.getParamEntry("dx", PrimitiveParam.class); |
---|
[84] | 51 | assertThat(dx).isInstanceOf(FloatParam.class); |
---|
| 52 | assertThat(schema.getNeuroClasses().size()).isEqualTo(21); |
---|
| 53 | assertThat(dx.getName()).isEqualTo("delta.x"); |
---|
| 54 | assertThat(dx.getMin(Double.class)).isEqualTo(-2.0, delta(0.0)); |
---|
[87] | 55 | |
---|
[90] | 56 | assertThat(schema.getRegistry().getFramsClass("n").getParamEntry("d", StringParam.class).getDef(String.class)).isEqualTo("N"); |
---|
[84] | 57 | } |
---|
[78] | 58 | |
---|
[90] | 59 | @Test(dependsOnMethods = "primitiveParam") |
---|
[84] | 60 | public void readF0() throws IOException, ParseException { |
---|
[88] | 61 | assertThat(schema.getFramsClass("p")).isInstanceOf(FramsClass.class); |
---|
[90] | 62 | assertThat(schema.getRegistry().getFramsClass("p").getParamEntry("as", FloatParam.class).getDef(Double.class)).isEqualTo(0.25, delta(0.0)); |
---|
[88] | 63 | |
---|
[84] | 64 | accesses = new F0Parser(schema, F0ParserTest.class.getResourceAsStream("/parsers/f0_example.txt")).parse(); |
---|
[78] | 65 | |
---|
[90] | 66 | assertThat(accesses.size()).isEqualTo(19); |
---|
[84] | 67 | assertThat(accesses.get(0).getSelected()).isInstanceOf(Model.class); |
---|
| 68 | assertThat(accesses.get(5).get("i", String.class)).isEqualTo("1,2,3,\"dsadsa,,,,"); |
---|
| 69 | assertThat(accesses.get(7).get("d", String.class)).isEqualTo("|:p=0.25,r=1"); |
---|
| 70 | assertThat(accesses.get(10).get("d", String.class)).isEqualTo("@:p=0.25"); |
---|
| 71 | } |
---|
[78] | 72 | |
---|
[84] | 73 | @Test(dependsOnMethods = {"readF0"}) |
---|
| 74 | public void stripAccessInterface() { |
---|
[90] | 75 | components = Util.stripAccessInterface(accesses, ModelComponent.class); |
---|
[78] | 76 | |
---|
[90] | 77 | assertThat(components.get(1)).isInstanceOf(Part.class); |
---|
| 78 | assertThat(components.get(4)).isInstanceOf(Joint.class); |
---|
| 79 | assertThat(components.get(6)).isInstanceOf(NeuroDefinition.class); |
---|
| 80 | assertThat(components.get(12)).isInstanceOf(NeuroConnection.class); |
---|
[84] | 81 | } |
---|
[78] | 82 | |
---|
[84] | 83 | @Test(dependsOnMethods = {"stripAccessInterface"}) |
---|
| 84 | public void buildModel() { |
---|
[90] | 85 | model = new ModelBuilder().addComponents(components).finish(); |
---|
[78] | 86 | |
---|
[84] | 87 | assertThat(model.getParts().size()).isEqualTo(3); |
---|
[90] | 88 | assertThat(model.getNeuroDefinitions().size()).isEqualTo(6); |
---|
[84] | 89 | assertThat(model.getJoints().size()).isEqualTo(2); |
---|
[90] | 90 | assertThat(model.getNeuroConnections().size()).isEqualTo(7); |
---|
[78] | 91 | |
---|
[90] | 92 | assertThat(model.getParts().get(1).getPosition().x).isEqualTo(2.0, delta(0.0)); |
---|
| 93 | assertThat(model.getParts().get(2).getPosition().sub(new Point3d(2.27236, -0.0792596, -0.958924)).length()).isLessThan(0.0001); |
---|
| 94 | assertThat(model.getParts().get(2).getOrientation().y.sub(new Point3d(0.870277, -0.404792, 0.280644)).length()).isLessThan(0.0001); |
---|
| 95 | |
---|
[84] | 96 | assertThat(model.getJoints().get(0).part1).isEqualTo(0); |
---|
| 97 | assertThat(model.getJoints().get(0).part2).isEqualTo(1); |
---|
[78] | 98 | |
---|
[90] | 99 | assertThat(model.getNeuroDefinitions().get(0).part).isEqualTo(1); |
---|
| 100 | assertThat(model.getNeuroDefinitions().get(0).joint).isEqualTo(-1); |
---|
| 101 | assertThat(model.getNeuroDefinitions().get(1).details).isEqualTo("|:p=0.25,r=1"); |
---|
| 102 | assertThat(model.getNeuroDefinitions().get(3).details).isEqualTo("N"); |
---|
| 103 | assertThat(model.getNeuroDefinitions().get(4).part).isEqualTo(-1); |
---|
| 104 | |
---|
| 105 | assertThat(model.getNeuroConnections().get(2).connectedNeuro).isEqualTo(0); |
---|
| 106 | assertThat(model.getNeuroConnections().get(5).weight).isEqualTo(5.6, delta(0.0)); |
---|
| 107 | |
---|
[84] | 108 | } |
---|
[79] | 109 | |
---|
[84] | 110 | @Test(dependsOnMethods = {"buildModel"}) |
---|
| 111 | public void print() throws Exception { |
---|
| 112 | ListSink sink = new ListSink(); |
---|
[88] | 113 | |
---|
[84] | 114 | new F0Writer(schema, model, sink).write(); |
---|
| 115 | |
---|
| 116 | assertThat(sink.getOut()).containsExactly( |
---|
| 117 | "p:", |
---|
| 118 | "p:2.0,i=,Vstyle=", |
---|
| 119 | "p:2.272364001928095,-0.07925961087140347,-0.9589242746631385,i=bla", |
---|
| 120 | "j:0,1,dx=2.0", |
---|
| 121 | "j:1,2,rx=8.0,5.0,6.0,dx=1.0,i=\"1,2,3,\\\"dsadsa,,,,\"", |
---|
| 122 | "n:p=1", |
---|
| 123 | "n:j=0,d=\"|:p=0.25,r=1\"", |
---|
| 124 | "n:j=0,d=G", |
---|
| 125 | "n:p=1", |
---|
| 126 | "n:j=0,d=@:p=0.25", |
---|
| 127 | "n:p=1,d=Nu", |
---|
[90] | 128 | "c:0,2", |
---|
| 129 | "c:0,2,5.4", |
---|
| 130 | "c:1,0", |
---|
| 131 | "c:2,0,3.4", |
---|
| 132 | "c:0,1,4.5", |
---|
| 133 | "c:1,0,5.6", |
---|
| 134 | "c:2,1", |
---|
[84] | 135 | "m:" |
---|
| 136 | ); |
---|
| 137 | |
---|
[78] | 138 | } |
---|
| 139 | } |
---|