Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixtures Factory does not generate values for Short type #41

Closed
marcello1975 opened this issue Nov 4, 2013 · 3 comments
Closed

Fixtures Factory does not generate values for Short type #41

marcello1975 opened this issue Nov 4, 2013 · 3 comments

Comments

@marcello1975
Copy link

Boa tarde,
Outro detalhe que não sei se interessa a vocês, é que para o caso da RandomFunction, se o cara informar um range fora dos valores MAX/MIN do tipo (Short por exemplo) ele retorna com perda de precisão, por exemplo, se o cara informar 99999 como Range máximo para um Short quando é dado "this.range.getEnd().shortValue()" ele retorna um valor negativo (-31073) por que esse número não cabe nos bits de um Short.
Eu entendo que "poxa se o cara está pedindo um Short porque vai colocar um range fora do tamanho do tipo???", mas caso queiram tratar isso seria tipo uma cereja no bolo... Talvez já no momento da criação do template lançar uma exceção tipo "RangeOutBoundException"... ai o cara nem consegue criar o template errado... pode ser preciosismo de minha parte, mas sei lá vejam ai que acham da ideia..

Att,
Marcello Ribeiro

I do have a simple java Object which has a property of Type Short.

What happens is that a simple "add/random" for this Short type property does not take any effect and the property is still null after gimme is called;

    Fixture.of(Trajeto.class).addTemplate("valid", new Rule() {
        {
            add("codPeriodo", random(Short.class, range(1, 99999)));
        }

    });
    Trajeto trajeto = Fixture.from(Trajeto.class).gimme("valid");

Does anyone have any words on this? or is it a really bug in the API?

@marcello1975
Copy link
Author

Same behaviour for BigDecimal and others...

@marcello1975
Copy link
Author

Problem is in this method when it relies that most of Number sons are assignable from Integer, Long and others:

@OverRide
@SuppressWarnings("unchecked")
public T generateValue() {
Object result = null;
Random random = new Random();

    if (this.dataset != null && this.dataset.length > 0) {
        result = this.dataset[random.nextInt(this.dataset.length)];

    } else if (this.functions != null && this.functions.length > 0) { 
        result = this.functions[random.nextInt(this.functions.length)].generateValue();

    } else if (this.type.isEnum()) {
        result = this.type.getEnumConstants()[random.nextInt(this.type.getEnumConstants().length)];

    } else if (this.type.isAssignableFrom(Integer.class)) {
        result = this.range == null ? random.nextInt() : (this.range.getStart().intValue() + (int)(Math.random() * (this.range.getEnd().intValue() - this.range.getStart().intValue()) + 1));

    } else if (this.type.isAssignableFrom(Long.class)) {
        result = this.range == null ? random.nextLong() : (this.range.getStart().longValue() + (long)(Math.random() * (this.range.getEnd().longValue() - this.range.getStart().longValue()) + 1));

    } else if (this.type.isAssignableFrom(Float.class)) {
        result = this.range == null ? random.nextFloat() : (this.range.getStart().floatValue() + (float)(Math.random() * (this.range.getEnd().floatValue() - this.range.getStart().floatValue()) + 1));

    } else if (this.type.isAssignableFrom(Double.class)) {
        result = this.range == null ? random.nextDouble() : (this.range.getStart().doubleValue() + (double)(Math.random() * (this.range.getEnd().doubleValue() - this.range.getStart().doubleValue()) + 1));

    } else if (this.type.isAssignableFrom(Boolean.class)) {
        result = random.nextBoolean();  
    }

    return (T) result;
}

@ahirata ahirata closed this as completed in 6253493 Nov 5, 2013
@ahirata
Copy link
Member

ahirata commented Nov 5, 2013

@marcello1975 thank you for reporting this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants