parallel_structure.spec.ts 5.72 KB
Newer Older
1
2
3
4
5
6
/**
 * IMPORTANT !
 * Décommenter temporairement la ligne suivante (import { } from "./mock_jasmine")
 * Pour exécuter ce code dans le débugger.
 * Faire de même avec le fichier test_func.ts
 */
Grand Francois's avatar
Grand Francois committed
7
// import { describe, expect, it, xdescribe, xit } from "../mock_jasmine";
8

9
import { ParamCalculability, ParamDefinition, ParamsEquation } from "../../src/param";
Dorchies David's avatar
Dorchies David committed
10
import { CreateStructure, loiAdmissibles, LoiDebit, StructureType } from "../../src/structure/factory_structure";
11
12
import { ParallelStructure } from "../../src/structure/parallel_structure";
import { ParallelStructureParams } from "../../src/structure/parallel_structure_params";
13
14
15
import { Structure } from "../../src/structure/structure";
import { Describer } from "../../src/util/describer";
import { EnumEx } from "../../src/util/enum";
Dorchies David's avatar
Dorchies David committed
16
import { MessageCode } from "../../src/util/message";
17
import { Result } from "../../src/util/result";
Dorchies David's avatar
Dorchies David committed
18
import { precDigits } from "../test_config";
19
20
21
22
23
24
25
26
import { checkResult } from "../test_func";
import { structTest } from "./structure_test";

const pstruct: ParallelStructure = new ParallelStructure(
    new ParallelStructureParams(30, 30, 15), // Q = 30, Z1 = 30, Z2 = 15
    false // debug
);

27
28
29
30
/*
 * Tests avec deux structures test identiques
 */

31
32
33
34
35
pstruct.addStructure(structTest);
pstruct.addStructure(structTest);

describe("Class ParallelStructure: ", () => {
    describe("Calc()", () => {
Dorchies David's avatar
Dorchies David committed
36
37
        it("should return 1 result", () => {
            const res: Result = pstruct.Calc("Q");
38
            expect(pstruct.Calc("Q").nbResultElements).toEqual(1);
Dorchies David's avatar
Dorchies David committed
39
        });
40
41
42
43
44
45
        itParallelStructure("Q", 30, 15);
        itParallelStructure("Z1", 30, 15);
        itParallelStructure("Z2", 15, 15);
    });
});

46
47
48
49
50
51
52
/**
 * Test sur ParallelStructure
 * @param sVarCalc Variable à calculer
 * @param rVcalc Valeur de référence à retrouver
 * @param Q Débit de chacune des structures (pour structures identiques uniquement)
 */
function itParallelStructure(sVarCalc: string, rVcalc: number, Q?: number) {
53
    it(`${sVarCalc} should be ${rVcalc}`, () => {
Dorchies David's avatar
Dorchies David committed
54
        checkResult(pstruct.Calc(sVarCalc), rVcalc);
55
    });
Dorchies David's avatar
Dorchies David committed
56
57
58
59
    if (Q !== undefined) {
        for (let i = 0; i < pstruct.structures.length; i++) {
            it(`ExtraResult[${i}.Q] should be ${Q}`, () => {
                expect(
60
                    pstruct.Calc(sVarCalc).resultElement.extraResults[`${i}.Q`]
Dorchies David's avatar
Dorchies David committed
61
62
63
64
                ).toBeCloseTo(Q, Math.max(0, precDigits - 1));
            });
            it(`ExtraResult[${i}.Q_Mode] should be 0`, () => {
                expect(
65
                    pstruct.Calc(sVarCalc).resultElement.extraResults[`${i}.Q_Mode`]
Dorchies David's avatar
Dorchies David committed
66
67
68
69
                ).toEqual(0);
            });
            it(`ExtraResult[${i}.Q_Regime] should be 0`, () => {
                expect(
70
                    pstruct.Calc(sVarCalc).resultElement.extraResults[`${i}.Q_Regime`]
Dorchies David's avatar
Dorchies David committed
71
72
73
74
75
                ).toEqual(0);
            });

        }
    }
76
}
77
78
79
80
81
82
83
84
85
86

/*
 * Tests avec toutes les équations et toutes les variables (cf. jalhyd#38)
 */

const ps2: ParallelStructure = new ParallelStructure(
    new ParallelStructureParams(0, 102, 101.5), // Q = 0, Z1 = 102, Z2 = 101.5
    false // debug
);

Dorchies David's avatar
Dorchies David committed
87
88
89
90
91
92
93
94
95
96
// tslint:disable-next-line:prefer-for-of
describe("Class ParallelStructure: ", () => {
    // Ajout d'une structure de chaque type dans ParallelStructure
    const iLoiDebits: number[] = [];
    const iStTypes: number[] = [];
    for (const s of EnumEx.getValues(StructureType)) {
        for (const la of loiAdmissibles[StructureType[s]]) {
            ps2.addStructure(CreateStructure(s, la, false));
            iLoiDebits.push(la);
            iStTypes.push(s);
Grand Francois's avatar
Grand Francois committed
97
98
        }
    }
99

Dorchies David's avatar
Dorchies David committed
100
    ps2.prms.Q.v = ps2.Calc("Q").vCalc;
101

Grand Francois's avatar
Grand Francois committed
102
    for (let i = 0; i < ps2.structures.length; i++) {
103
        const st: Structure = ps2.structures[i];
Dorchies David's avatar
Dorchies David committed
104
        describe(`this.structures[${i}]: ${StructureType[iStTypes[i]]} Structure${LoiDebit[iLoiDebits[i]]}: `, () => {
105
            for (const prm of st.prms) {
Dorchies David's avatar
Dorchies David committed
106
107
108
109
110
                if (
                    prm.calculability === ParamCalculability.DICHO &&
                    prm.symbol !== "Z1" && prm.symbol !== "Z2"
                ) {
                    const ref: number = prm.v;
111
112
113
114
115
116
                    if (prm.symbol === "W" && prm.v === Infinity) {
                        // Le calcul de l'ouverture sur les seuils doit renvoyer une exception (cas impossible)
                        it(`Calc(${prm.symbol}) should return exception`, () => {
                            expect(
                                () => { ps2.Calc(i + "." + prm.symbol); }
                            ).toThrow(new Error("Structure:Calc : Calcul de W impossible sur un seuil"));
117
                        });
Dorchies David's avatar
Dorchies David committed
118
119
120
121
122
123
124
125
126
127
128
129
                    } else if (
                        iStTypes[i] === StructureType.VanneRectangulaire &&
                        !ps2.structures[i].isZDVcalculable &&
                        prm.symbol === "ZDV"
                    ) {
                        // Les lois CEM88D et CUNGE80 ne font pas intervenir ZDV dans le calcul d'un orifice noyé
                        // TODO Il faudrait générer une erreur pour dire que le résultat est indéfini
                        it(`Calc(${prm.symbol}) should return an error`, () => {
                            expect(
                                ps2.Calc(i + "." + prm.symbol).code
                            ).toBe(MessageCode.ERROR_STRUCTURE_ZDV_PAS_CALCULABLE);
                        });
130
                    } else {
Dorchies David's avatar
Dorchies David committed
131
132
133
134
                        // Cas normal : On teste la valeur calculée
                        it(`Calc(${prm.symbol}) should return ${ref}`, () => {
                            checkResult(ps2.Calc(i + "." + prm.symbol), ref);
                        });
135
                    }
Dorchies David's avatar
Dorchies David committed
136
                    prm.v = ref; // Go back to initial value for following tests
137
138
139
140
141
                }
            }
        });
    }
});