diff --git a/src/StringIndexer.h b/src/StringIndexer.h index dc3777c..8d26de9 100644 --- a/src/StringIndexer.h +++ b/src/StringIndexer.h @@ -39,7 +39,7 @@ class StringIndexer return it->first; } } - for(index_t index=0; index<255; index++) + for(index_t index=1; index; index++) { if (strings.find(index)==strings.end()) { @@ -80,6 +80,8 @@ class StringIndexer } } + static uint16_t count() { return strings.size(); } + private: static std::map strings; }; @@ -98,6 +100,8 @@ class IndexedString index=StringIndexer::strToIndex(str, len); } + IndexedString(const std::string& str) : IndexedString(str.c_str(), str.length()) {}; + ~IndexedString() { StringIndexer::release(index); } IndexedString& operator=(const IndexedString& source) @@ -112,6 +116,11 @@ class IndexedString return i1.index < i2.index; } + friend bool operator==(const IndexedString& i1, const IndexedString& i2) + { + return i1.index == i2.index; + } + const std::string& str() const { return StringIndexer::str(index); } const StringIndexer::index_t& getIndex() const { return index; } diff --git a/src/TinyMqtt.h b/src/TinyMqtt.h index 5385e61..a1a6885 100644 --- a/src/TinyMqtt.h +++ b/src/TinyMqtt.h @@ -120,13 +120,15 @@ class MqttClient FlagReserved = 1 }; public: - MqttClient(MqttBroker* brk = nullptr, const std::string& id=""); + /** Constructor. If broker is not null, this is the adress of a local broker. + If you want to connect elsewhere, leave broker null and use connect() **/ + MqttClient(MqttBroker* broker = nullptr, const std::string& id=""); MqttClient(const std::string& id) : MqttClient(nullptr, id){} ~MqttClient(); void connect(MqttBroker* parent); - void connect(std::string broker, uint16_t port, uint16_t ka=10); + void connect(std::string broker, uint16_t port, uint16_t keep_alive = 10); bool connected() { return (parent!=nullptr and client==nullptr) or @@ -135,8 +137,9 @@ class MqttClient { if (client) client->write(buf, length); } const std::string& id() const { return clientId; } - void id(std::string& new_id) { clientId = new_id; } + // void id(std::string& new_id) { clientId = new_id; } + /** Should be called in main loop() */ void loop(); void close(bool bSendDisconnect=true); void setCallback(CallBack fun) {callback=fun; }; @@ -173,7 +176,8 @@ class MqttClient Serial << "]" << endl; } - static long counter; // Number of messages sent + /** Count the number of messages that have been sent **/ + static long counter; private: MqttError sendTopic(const Topic& topic, MqttMessage::Type type, uint8_t qos); diff --git a/tests/string-indexer-tests/Makefile b/tests/string-indexer-tests/Makefile new file mode 100644 index 0000000..a118875 --- /dev/null +++ b/tests/string-indexer-tests/Makefile @@ -0,0 +1,6 @@ +# See https://github.com/bxparks/EpoxyDuino for documentation about this +# Makefile to compile and run Arduino programs natively on Linux or MacOS. + +APP_NAME := string-indexer-tests +ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt EspMock +include ../../../EpoxyDuino/EpoxyDuino.mk diff --git a/tests/string-indexer-tests/string-indexer-tests.ino b/tests/string-indexer-tests/string-indexer-tests.ino new file mode 100644 index 0000000..b7d76a8 --- /dev/null +++ b/tests/string-indexer-tests/string-indexer-tests.ino @@ -0,0 +1,123 @@ +#include +#include +#include + +/** + * TinyMqtt local unit tests. + * + * Clients are connected to pseudo remote broker + * The remote will be 127.0.0.1:1883 + * We are using 127.0.0.1 because this is simpler to test with a single ESP + * Also, this will allow to mock and thus run Action on github + **/ + +using namespace std; + +MqttBroker broker(1883); + +std::map> published; // map[client_id] => map[topic] = count + +void onPublish(const MqttClient* srce, const Topic& topic, const char* payload, size_t length) +{ + if (srce) + published[srce->id()][topic]++; +} + +test(indexer_empty) +{ + assertTrue(StringIndexer::count()==0); +} + +test(indexer_strings_deleted_should_empty_indexer) +{ + assertTrue(StringIndexer::count()==0); + { + IndexedString one("one"); + assertTrue(StringIndexer::count()==1); + IndexedString two("two"); + assertTrue(StringIndexer::count()==2); + IndexedString three("three"); + assertTrue(StringIndexer::count()==3); + IndexedString four("four"); + assertTrue(StringIndexer::count()==4); + } + assertTrue(StringIndexer::count()==0); +} + +test(indexer_same_strings_count_as_one) +{ + IndexedString one ("one"); + IndexedString two ("one"); + IndexedString three("one"); + IndexedString fourt("one"); + + assertTrue(StringIndexer::count()==1); +} + +test(indexer_size_of_indexed_string) +{ + assertEqual(sizeof(IndexedString), (size_t)1); +} + +test(indexer_different_strings_are_different) +{ + IndexedString one("one"); + IndexedString two("two"); + + assertFalse(one == two); +} + +test(indexer_same_strings_should_equal) +{ + IndexedString one("one"); + IndexedString two("one"); + + assertTrue(one == two); +} + +test(indexer_indexed_operator_eq) +{ + IndexedString one("one"); + { + IndexedString same = one; + assertTrue(one == same); + assertTrue(StringIndexer::count()==1); + } + assertTrue(StringIndexer::count()==1); +} + +test(indexer_get_string) +{ + std::string sone("one"); + IndexedString one(sone); + + assertTrue(sone==one.str()); +} + +test(indexer_get_index) +{ + IndexedString one1("one"); + IndexedString one2("one"); + IndexedString two1("two"); + IndexedString two2("two"); + + assertTrue(one1.getIndex() == one2.getIndex()); + assertTrue(two1.getIndex() == two2.getIndex()); + assertTrue(one1.getIndex() != two1.getIndex()); +} + +//---------------------------------------------------------------------------- +// setup() and loop() +void setup() { + delay(1000); + Serial.begin(115200); + while(!Serial); + + Serial.println("=============[ TinyMqtt StringIndexer TESTS ]========================"); +} + +void loop() { + aunit::TestRunner::run(); + + if (Serial.available()) ESP.reset(); +}