File: test_MassCollection.cpp

package info (click to toggle)
libxpertmass 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,908 kB
  • sloc: cpp: 50,586; xml: 2,193; python: 417; ansic: 70; makefile: 33
file content (169 lines) | stat: -rw-r--r-- 5,739 bytes parent folder | download
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/////////////////////// Qt includes
#include <QDebug>
#include <QString>
#include <QDir>


/////////////////////// Catch2 includes
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>


/////////////////////// Local includes
#include "TestUtils.hpp"
#include <libXpertMass/MassCollection.hpp>

namespace MsXpS
{
namespace libXpertMassCore
{

SCENARIO("MassCollection objects can be constructed with a name and masses",
         "[MassCollection]")
{
  GIVEN(
    "A masses-as-text string and a vector of double values representing "
    "exactly the same data")
  {
    QString masses_as_text("123.321\n1234.4321\n12345.54321\n123456.654321");
    std::vector<double> masses{123.321, 1234.4321, 12345.54321, 123456.654321};

    WHEN("MassCollection instances are created with these")
    {
      // Trick to test setName()
      MassCollection mass_collection_1("other_mono_masses", masses_as_text);
      mass_collection_1.setName("mono_masses");
      mass_collection_1.setComment("This is a comment");

      MassCollection mass_collection_2("mono_masses", masses);
      mass_collection_2.setComment("This is a comment");

      THEN("Both MassCollection instances should be identical")
      {
        REQUIRE(mass_collection_1.isValid());
        REQUIRE(mass_collection_2.isValid());

        REQUIRE(mass_collection_1.getName().toStdString() == "mono_masses");
        REQUIRE(mass_collection_1.getComment().toStdString() ==
                "This is a comment");

        REQUIRE(mass_collection_2.getName().toStdString() == "mono_masses");
        REQUIRE(mass_collection_2.getComment().toStdString() ==
                "This is a comment");

        REQUIRE(mass_collection_1.size() == mass_collection_2.size());

        for(std::size_t iter = 0; iter < mass_collection_1.size(); ++iter)
          REQUIRE(mass_collection_1.getMassesCstRef().at(iter) ==
                  mass_collection_2.getMassesCstRef().at(iter));
      }

      WHEN("Copy-constructing or assigning new MassCollection objects")
      {
        MassCollection another_other_mass_collection(mass_collection_1);
        MassCollection other_mass_collection("different_name");
        other_mass_collection = another_other_mass_collection;

        THEN("All the MassCollection instances should be identical")
        {
          REQUIRE(other_mass_collection.isValid());

          REQUIRE(mass_collection_1.getName().toStdString() == "mono_masses");
          REQUIRE(mass_collection_1.getComment().toStdString() ==
                  "This is a comment");

          REQUIRE(other_mass_collection.size() == mass_collection_1.size());

          for(std::size_t iter = 0; iter < mass_collection_1.size(); ++iter)
            {
              // Trick to use both versio of getMassesRef:
              REQUIRE(other_mass_collection.getMassesCstRef().at(iter) ==
                      mass_collection_1.getMassesRef().at(iter));
              // Use also the other function getMassAtIndex()
              REQUIRE(other_mass_collection.getMassAtIndex(iter) ==
                      mass_collection_1.getMassAtIndex(iter));
            }
        }
      }

      WHEN("Requesting the masses in the form of a text string")
      {
        QString masses_as_text = mass_collection_1.massesToText();

        THEN("The text should be as expected")
        {
          REQUIRE(masses_as_text.toStdString() ==
                  "123.32100\n1234.43210\n12345.54321\n123456.65432\n");
        }
      }

      WHEN("When adding one mass to the masses")
      {
        mass_collection_1.addMass(1.0000000000);
        QString masses_as_text = mass_collection_1.massesToText();

        THEN("The new text should be as expected")
        {
          REQUIRE(masses_as_text.toStdString() ==
                  "124.32100\n1235.43210\n12346.54321\n123457.65432\n");
        }
      }

      WHEN("When asking for MassCollection validation")
      {
        ErrorList error_list;
        bool is_valid = mass_collection_1.validate(&error_list);

        THEN("The MassCollection instance should validate succesfully")
        {
          REQUIRE(is_valid);
        }
      }

      WHEN("Sorting masses descending")
      {
        mass_collection_1.sortDescending();

        THEN("The values should sort correctly")
        {
          REQUIRE_THAT(mass_collection_1.getMassesCstRef().front(),
                       Catch::Matchers::WithinAbs(123456.654321, 0.0000000001));

          REQUIRE_THAT(mass_collection_1.getMassesCstRef().back(),
                       Catch::Matchers::WithinAbs(123.321, 0.0000000001));
        }

        AND_WHEN("Sorted back in ascending order")
        {
          mass_collection_1.sortAscending();
          THEN("The values should sort correctly")
          {
            REQUIRE_THAT(mass_collection_1.getMassesCstRef().front(),
                         Catch::Matchers::WithinAbs(123.321, 0.0000000001));

            REQUIRE_THAT(
              mass_collection_1.getMassesCstRef().back(),
              Catch::Matchers::WithinAbs(123456.654321, 0.0000000001));
          }

          AND_WHEN("removeLessThan() is called")
          {
            mass_collection_1.removeLessThan(500);
            THEN("The remainig values should be correct")
            {
              REQUIRE_THAT(mass_collection_1.getMassesCstRef().front(),
                           Catch::Matchers::WithinAbs(1234.4321, 0.0000000001));

              REQUIRE_THAT(
                mass_collection_1.getMassesCstRef().back(),
                Catch::Matchers::WithinAbs(123456.654321, 0.0000000001));
            }
          }
        }
      }
    }
  }
}

} // namespace libXpertMassCore
} // namespace MsXpS