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

bblanchon / ArduinoJson / 5665912587

pending completion
5665912587

push

github

bblanchon
Tests: make allocator assertions more readable

3421 of 3435 relevant lines covered (99.59%)

8816.96 hits per line

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

98.73
/src/ArduinoJson/Collection/CollectionImpl.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2023, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

7
#include <ArduinoJson/Collection/CollectionData.hpp>
8
#include <ArduinoJson/Memory/Alignment.hpp>
9
#include <ArduinoJson/Strings/StringAdapters.hpp>
10
#include <ArduinoJson/Variant/VariantCompare.hpp>
11
#include <ArduinoJson/Variant/VariantData.hpp>
12

13
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
14

15
inline CollectionIterator::CollectionIterator(VariantSlot* slot, SlotId slotId)
78,054✔
16
    : slot_(slot), currentId_(slotId) {
78,054✔
17
  nextId_ = slot_ ? slot_->next() : NULL_SLOT;
78,054✔
18
}
78,054✔
19

20
inline const char* CollectionIterator::key() const {
12,573✔
21
  ARDUINOJSON_ASSERT(slot_ != nullptr);
22
  return slot_->key();
12,573✔
23
}
24

25
inline void CollectionIterator::setKey(const char* s) {
698✔
26
  ARDUINOJSON_ASSERT(slot_ != nullptr);
27
  ARDUINOJSON_ASSERT(s != nullptr);
28
  return slot_->setKey(s);
698✔
29
}
30

31
inline void CollectionIterator::setKey(StringNode* s) {
1,382✔
32
  ARDUINOJSON_ASSERT(slot_ != nullptr);
33
  ARDUINOJSON_ASSERT(s != nullptr);
34
  return slot_->setKey(s);
1,382✔
35
}
36

37
inline bool CollectionIterator::ownsKey() const {
455✔
38
  ARDUINOJSON_ASSERT(slot_ != nullptr);
39
  return slot_->ownsKey();
455✔
40
}
41

42
inline void CollectionIterator::next(const ResourceManager* resources) {
275,430✔
43
  ARDUINOJSON_ASSERT(currentId_ != NULL_SLOT);
44
  slot_ = resources->getSlot(nextId_);
275,430✔
45
  currentId_ = nextId_;
275,430✔
46
  if (slot_)
275,430✔
47
    nextId_ = slot_->next();
271,650✔
48
}
275,430✔
49

50
inline CollectionData::iterator CollectionData::addSlot(
71,046✔
51
    ResourceManager* resources) {
52
  auto slot = resources->allocSlot();
71,046✔
53
  if (!slot)
71,046✔
54
    return {};
19✔
55
  if (tail_ != NULL_SLOT) {
71,027✔
56
    auto tail = resources->getSlot(tail_);
68,890✔
57
    tail->setNext(slot.id());
68,890✔
58
    tail_ = slot.id();
68,890✔
59
  } else {
60
    head_ = slot.id();
2,137✔
61
    tail_ = slot.id();
2,137✔
62
  }
63
  return iterator(slot, slot.id());
71,027✔
64
}
65

66
inline void CollectionData::clear(ResourceManager* resources) {
52✔
67
  auto next = head_;
52✔
68
  while (next != NULL_SLOT) {
197✔
69
    auto currId = next;
145✔
70
    auto slot = resources->getSlot(next);
145✔
71
    next = slot->next();
145✔
72
    releaseSlot(SlotWithId(slot, currId), resources);
145✔
73
  }
74

75
  head_ = NULL_SLOT;
52✔
76
  tail_ = NULL_SLOT;
52✔
77
}
52✔
78

79
inline SlotWithId CollectionData::getPreviousSlot(
39✔
80
    VariantSlot* target, const ResourceManager* resources) const {
81
  auto prev = SlotWithId();
39✔
82
  auto currentId = head_;
39✔
83
  while (currentId != NULL_SLOT) {
66✔
84
    auto currentSlot = resources->getSlot(currentId);
66✔
85
    if (currentSlot == target)
66✔
86
      return prev;
39✔
87
    prev = SlotWithId(currentSlot, currentId);
27✔
88
    currentId = currentSlot->next();
27✔
89
  }
90
  return SlotWithId();
×
91
}
92

93
inline void CollectionData::remove(iterator it, ResourceManager* resources) {
41✔
94
  if (it.done())
41✔
95
    return;
2✔
96
  auto curr = it.slot_;
39✔
97
  auto prev = getPreviousSlot(curr, resources);
39✔
98
  auto next = curr->next();
39✔
99
  if (prev)
39✔
100
    prev->setNext(next);
23✔
101
  else
102
    head_ = next;
16✔
103
  if (next == NULL_SLOT)
39✔
104
    tail_ = prev.id();
19✔
105
  releaseSlot({it.slot_, it.currentId_}, resources);
39✔
106
}
107

108
inline size_t CollectionData::nesting(const ResourceManager* resources) const {
16✔
109
  size_t maxChildNesting = 0;
16✔
110
  for (auto it = createIterator(resources); !it.done(); it.next(resources)) {
22✔
111
    size_t childNesting = it->nesting(resources);
6✔
112
    if (childNesting > maxChildNesting)
6✔
113
      maxChildNesting = childNesting;
4✔
114
  }
115
  return maxChildNesting + 1;
16✔
116
}
117

118
inline size_t CollectionData::size(const ResourceManager* resources) const {
356✔
119
  size_t count = 0;
356✔
120
  for (auto it = createIterator(resources); !it.done(); it.next(resources))
132,049✔
121
    count++;
131,693✔
122
  return count;
356✔
123
}
124

125
inline void CollectionData::releaseSlot(SlotWithId slot,
184✔
126
                                        ResourceManager* resources) {
127
  if (slot->ownsKey())
184✔
128
    resources->dereferenceString(slot->key());
5✔
129
  slot->data()->setNull(resources);
184✔
130
  resources->freeSlot(slot);
184✔
131
}
184✔
132

133
ARDUINOJSON_END_PRIVATE_NAMESPACE
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