• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.78
/src/tests/test_mp.cpp
1
/*
2
* (C) 2016 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "tests.h"
8

9
#if defined(BOTAN_HAS_BIGINT_MP)
10
   #include <botan/internal/mp_core.h>
11
#endif
12

13
namespace Botan_Tests {
14

15
namespace {
16

17
#if defined(BOTAN_HAS_BIGINT_MP)
18

19
class MP_Unit_Tests final : public Test {
×
20
   public:
21
      std::vector<Test::Result> run() override {
1✔
22
         std::vector<Test::Result> results;
1✔
23

24
         results.push_back(test_cnd_swap());
2✔
25
         results.push_back(test_cnd_add());
2✔
26
         results.push_back(test_cnd_sub());
2✔
27
         results.push_back(test_cnd_abs());
2✔
28

29
         return results;
1✔
30
      }
×
31

32
   private:
33
      static Result test_cnd_add() {
1✔
34
         Result result("bigint_cnd_add");
1✔
35

36
         const Botan::word max = Botan::MP_WORD_MAX;
1✔
37

38
         Botan::word a = 2;
1✔
39
         Botan::word c = Botan::bigint_cnd_add(0, &a, &max, 1);
1✔
40

41
         result.test_int_eq(a, 2, "No op");
1✔
42
         result.test_int_eq(c, 0, "No op");
1✔
43

44
         c = Botan::bigint_cnd_add(1, &a, &max, 1);
1✔
45

46
         result.test_int_eq(a, 1, "Add");
1✔
47
         result.test_int_eq(c, 1, "Carry");
1✔
48

49
         // TODO more tests
50

51
         return result;
1✔
52
      }
×
53

54
      static Result test_cnd_sub() {
1✔
55
         Result result("bigint_cnd_sub");
1✔
56

57
         Botan::word a = 2;
1✔
58
         Botan::word b = 3;
1✔
59
         Botan::word c = Botan::bigint_cnd_sub(0, &a, &b, 1);
1✔
60

61
         result.test_int_eq(a, 2, "No op");
1✔
62
         result.test_int_eq(c, 0, "No op");
1✔
63

64
         c = Botan::bigint_cnd_sub(1, &a, &b, 1);
1✔
65

66
         result.test_int_eq(a, Botan::MP_WORD_MAX, "Sub");
1✔
67
         result.test_int_eq(c, 1, "Borrow");
1✔
68

69
         return result;
1✔
70
      }
×
71

72
      static Result test_cnd_abs() {
1✔
73
         Result result("bigint_cnd_abs");
1✔
74

75
         Botan::word x1 = Botan::MP_WORD_MAX;
1✔
76
         Botan::bigint_cnd_abs(1, &x1, 1);
1✔
77
         result.test_int_eq(x1, 1, "Abs");
1✔
78

79
         x1 = 0;
1✔
80
         Botan::bigint_cnd_abs(1, &x1, 1);
1✔
81
         result.test_int_eq(x1, 0, "Abs");
1✔
82

83
         x1 = 1;
1✔
84
         Botan::bigint_cnd_abs(1, &x1, 1);
1✔
85
         result.test_int_eq(x1, Botan::MP_WORD_MAX, "Abs");
1✔
86

87
         x1 = 1;
1✔
88
         Botan::bigint_cnd_abs(0, &x1, 1);
1✔
89
         result.test_int_eq(x1, 1, "No change");
1✔
90

91
         Botan::word x2[2] = {Botan::MP_WORD_MAX, Botan::MP_WORD_MAX};
1✔
92

93
         Botan::bigint_cnd_abs(1, x2, 2);
1✔
94
         result.test_int_eq(x2[0], 1, "Abs");
1✔
95
         result.test_int_eq(x2[1], 0, "Abs");
1✔
96

97
         return result;
1✔
98
      }
×
99

100
      static Result test_cnd_swap() {
1✔
101
         Result result("bigint_cnd_swap");
1✔
102

103
         // null with zero length is ok
104
         Botan::bigint_cnd_swap(0, nullptr, nullptr, 0);
1✔
105
         Botan::bigint_cnd_swap(1, nullptr, nullptr, 0);
1✔
106

107
         Botan::word x1 = 5, y1 = 9;
1✔
108

109
         Botan::bigint_cnd_swap(0, &x1, &y1, 1);
1✔
110
         result.test_int_eq(x1, 5, "No swap");
1✔
111
         Botan::bigint_cnd_swap(1, &x1, &y1, 1);
1✔
112
         result.test_int_eq(x1, 9, "Swap");
1✔
113

114
         Botan::word x5[5] = {0, 1, 2, 3, 4};
1✔
115
         Botan::word y5[5] = {3, 2, 1, 0, 9};
1✔
116

117
         // Should only modify first four
118
         Botan::bigint_cnd_swap(1, x5, y5, 4);
1✔
119

120
         for(size_t i = 0; i != 4; ++i) {
5✔
121
            result.test_int_eq(x5[i], 3 - i, "Swap x5");
4✔
122
         }
123
         result.test_int_eq(x5[4], 4, "Not touched");
1✔
124

125
         for(size_t i = 0; i != 4; ++i) {
5✔
126
            result.test_int_eq(y5[i], i, "Swap y5");
4✔
127
         }
128
         result.test_int_eq(y5[4], 9, "Not touched");
1✔
129

130
         return result;
1✔
131
      }
×
132
};
133

134
BOTAN_REGISTER_TEST("math", "mp_unit", MP_Unit_Tests);
135

136
#endif
137

138
}
139

140
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc