Skip to content

Commit

Permalink
Improve Composite Pattern Test
Browse files Browse the repository at this point in the history
  • Loading branch information
hextriclosan committed Oct 15, 2024
1 parent 3bb2d40 commit 7b75947
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 9 deletions.
7 changes: 6 additions & 1 deletion jdescriptor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn default_value(type_descriptor: &TypeDescriptor) -> Vec<i32> {
Byte | Char | Int | Short | Boolean => vec![0],
Long => vec![0, 0],
Float => from_f32(0.0),
Double => todo!(),
Double => from_f64(0.0),
Void => panic!("field can't be a void type"),
Array(_, _) => vec![0],
Object(_) => vec![0],
Expand Down Expand Up @@ -180,6 +180,11 @@ fn from_f32(value: f32) -> Vec<i32> {
vec![value.to_bits() as i32]
}

fn from_f64(value: f64) -> Vec<i32> {
let bits = value.to_bits();
vec![(bits >> 32) as i32, bits as i32]
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
17 changes: 9 additions & 8 deletions tests/test_data/CompositePattern.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package samples.inheritance.interfaces.compositepattern;

import java.util.ArrayList;
import java.util.List;

public class CompositePattern {
public static void main(String[] args) {
Composite outerComposite = new Composite(5);
Composite outerComposite = new Composite();
outerComposite.addUnit(new Zealot());
outerComposite.addUnit(new Zealot());
outerComposite.addUnit(new DarkTemplar());
outerComposite.addUnit(new DarkTemplar());

Composite innerComposite = new Composite(3);
Composite innerComposite = new Composite();
innerComposite.addUnit(new Zealot());
innerComposite.addUnit(new Unit() {
@Override
Expand Down Expand Up @@ -44,16 +47,14 @@ public int attack() {

class Composite implements Unit {

private final Unit[] units;
private int currentIndex;
private final List<Unit> units;

public Composite(int size) {
units = new Unit[size];
currentIndex = 0;
public Composite() {
units = new ArrayList<>();
}

public void addUnit(Unit unit) {
units[currentIndex++] = unit;
units.add(unit);
}

@Override
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 7b75947

Please sign in to comment.