| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*************************************** | ||
| 2 | Auteur : Thibaut Oprinsen | ||
| 3 | Mail : thibaut.oprinsen@lapp.in2p3.fr | ||
| 4 | Licence : CeCILL-C | ||
| 5 | ****************************************/ | ||
| 6 | |||
| 7 | #include "NanobindTraitDataStream.h" | ||
| 8 | |||
| 9 | ///Constructor of NanobindTraitDataStream | ||
| 10 | 1 | NanobindTraitDataStream::NanobindTraitDataStream() | |
| 11 | 1 | : PAbstractNanobindTraitBackend() | |
| 12 | 1 | {} | |
| 13 | |||
| 14 | ///Destructor of NanobindTraitDataStream | ||
| 15 | 2 | NanobindTraitDataStream::~NanobindTraitDataStream(){} | |
| 16 | |||
| 17 | /// @brief registration of serialization methods | ||
| 18 | /// @param fs stream to write to | ||
| 19 | /// @param classConfig config of the class | ||
| 20 | /// @param mode generator mode | ||
| 21 | 2 | void registerDeserializationMethods(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode){ | |
| 22 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 2 (3→4) taken 2 times.
|
2 | PString name(classConfig.getName()); |
| 23 |
4/4✓ Branch 0 (4→5) taken 2 times.
✓ Branch 2 (5→6) taken 2 times.
✓ Branch 4 (6→7) taken 2 times.
✓ Branch 6 (7→8) taken 2 times.
|
2 | fs << std::endl << std::endl << "\t\t///Deserialization method for " << name; |
| 24 |
4/4✓ Branch 0 (8→9) taken 2 times.
✓ Branch 2 (9→10) taken 2 times.
✓ Branch 4 (10→11) taken 2 times.
✓ Branch 6 (11→12) taken 2 times.
|
2 | fs << std::endl << "\t\t.def(\"from_bytes\", []( " << name << " & self, const nb::bytearray &b) -> bool {"; |
| 25 |
2/2✓ Branch 0 (12→13) taken 2 times.
✓ Branch 2 (13→14) taken 2 times.
|
2 | fs << std::endl << "\t\t\tDataStreamIter iter((DataStreamIter)b.data());;"; |
| 26 |
2/2✓ Branch 0 (14→15) taken 2 times.
✓ Branch 2 (15→16) taken 2 times.
|
2 | fs << std::endl << "\t\t\treturn data_message_load(iter, self);"; |
| 27 |
2/2✓ Branch 0 (16→17) taken 2 times.
✓ Branch 2 (17→18) taken 2 times.
|
2 | fs << std::endl << "\t\t}, "; |
| 28 |
4/4✓ Branch 0 (18→19) taken 2 times.
✓ Branch 2 (19→20) taken 2 times.
✓ Branch 4 (20→21) taken 2 times.
✓ Branch 6 (21→22) taken 2 times.
|
2 | fs << std::endl << "\t\t\"Load a " << name << " from a bytearray\""; |
| 29 |
2/2✓ Branch 0 (22→23) taken 2 times.
✓ Branch 2 (23→24) taken 2 times.
|
2 | fs << std::endl << "\t\t)"; |
| 30 | 2 | } | |
| 31 | |||
| 32 | /// @brief registration of serialization methods | ||
| 33 | /// @param fs stream to write to | ||
| 34 | /// @param classConfig config of the class | ||
| 35 | /// @param mode generator mode | ||
| 36 | 2 | void registerSerializationMethods(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode){ | |
| 37 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 2 (3→4) taken 2 times.
|
2 | PString name(classConfig.getName()); |
| 38 |
4/4✓ Branch 0 (4→5) taken 2 times.
✓ Branch 2 (5→6) taken 2 times.
✓ Branch 4 (6→7) taken 2 times.
✓ Branch 6 (7→8) taken 2 times.
|
2 | fs << std::endl << std::endl << "\t\t///Serialization method for " << name; |
| 39 |
4/4✓ Branch 0 (8→9) taken 2 times.
✓ Branch 2 (9→10) taken 2 times.
✓ Branch 4 (10→11) taken 2 times.
✓ Branch 6 (11→12) taken 2 times.
|
2 | fs << std::endl << "\t\t.def(\"to_bytes\", [](const " << name << " & self) -> nb::bytearray {"; |
| 40 |
2/2✓ Branch 0 (12→13) taken 2 times.
✓ Branch 2 (13→14) taken 2 times.
|
2 | fs << std::endl << "\t\t\tsize_t size = data_size(self);"; |
| 41 |
2/2✓ Branch 0 (14→15) taken 2 times.
✓ Branch 2 (15→16) taken 2 times.
|
2 | fs << std::endl << "\t\t\tnb::bytearray msg(NULL, size);"; |
| 42 |
2/2✓ Branch 0 (16→17) taken 2 times.
✓ Branch 2 (17→18) taken 2 times.
|
2 | fs << std::endl << "\t\t\tDataStreamIter iter((DataStreamIter)msg.data());"; |
| 43 |
2/2✓ Branch 0 (18→19) taken 2 times.
✓ Branch 2 (19→20) taken 2 times.
|
2 | fs << std::endl << "\t\t\tbool success = data_message_save(iter, self);"; |
| 44 |
2/2✓ Branch 0 (20→21) taken 2 times.
✓ Branch 2 (21→22) taken 2 times.
|
2 | fs << std::endl << "\t\t\tif(success){"; |
| 45 |
2/2✓ Branch 0 (22→23) taken 2 times.
✓ Branch 2 (23→24) taken 2 times.
|
2 | fs << std::endl << "\t\t\t\treturn msg;"; |
| 46 |
2/2✓ Branch 0 (24→25) taken 2 times.
✓ Branch 2 (25→26) taken 2 times.
|
2 | fs << std::endl << "\t\t\t}else{"; |
| 47 |
4/4✓ Branch 0 (26→27) taken 2 times.
✓ Branch 2 (27→28) taken 2 times.
✓ Branch 4 (28→29) taken 2 times.
✓ Branch 6 (29→30) taken 2 times.
|
2 | fs << std::endl << "\t\t\t\tthrow nb::buffer_error(\"Failed to serialize " << name << "\");"; |
| 48 |
2/2✓ Branch 0 (30→31) taken 2 times.
✓ Branch 2 (31→32) taken 2 times.
|
2 | fs << std::endl << "\t\t\t}"; |
| 49 |
2/2✓ Branch 0 (32→33) taken 2 times.
✓ Branch 2 (33→34) taken 2 times.
|
2 | fs << std::endl << "\t\t}, "; |
| 50 |
4/4✓ Branch 0 (34→35) taken 2 times.
✓ Branch 2 (35→36) taken 2 times.
✓ Branch 4 (36→37) taken 2 times.
✓ Branch 6 (37→38) taken 2 times.
|
2 | fs << std::endl << "\t\t\"Serialize the " << name << " to bytearray\""; |
| 51 |
2/2✓ Branch 0 (38→39) taken 2 times.
✓ Branch 2 (39→40) taken 2 times.
|
2 | fs << std::endl << "\t\t)"; |
| 52 | 2 | } | |
| 53 | |||
| 54 | /// @brief registration of size method | ||
| 55 | /// @param fs stream to write to | ||
| 56 | /// @param classConfig config of the class | ||
| 57 | /// @param mode generator mode | ||
| 58 | 2 | void registerSizeMethod(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode){ | |
| 59 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 2 (3→4) taken 2 times.
|
2 | PString name(classConfig.getName()); |
| 60 |
4/4✓ Branch 0 (4→5) taken 2 times.
✓ Branch 2 (5→6) taken 2 times.
✓ Branch 4 (6→7) taken 2 times.
✓ Branch 6 (7→8) taken 2 times.
|
2 | fs << std::endl << std::endl << "\t\t///Size method for " << name; |
| 61 |
4/4✓ Branch 0 (8→9) taken 2 times.
✓ Branch 2 (9→10) taken 2 times.
✓ Branch 4 (10→11) taken 2 times.
✓ Branch 6 (11→12) taken 2 times.
|
2 | fs << std::endl << "\t\t.def(\"get_size\", [](const " << name << " & self) -> size_t {"; |
| 62 |
2/2✓ Branch 0 (12→13) taken 2 times.
✓ Branch 2 (13→14) taken 2 times.
|
2 | fs << std::endl << "\t\t\treturn data_size(self);"; |
| 63 |
2/2✓ Branch 0 (14→15) taken 2 times.
✓ Branch 2 (15→16) taken 2 times.
|
2 | fs << std::endl << "\t\t}, "; |
| 64 |
4/4✓ Branch 0 (16→17) taken 2 times.
✓ Branch 2 (17→18) taken 2 times.
✓ Branch 4 (18→19) taken 2 times.
✓ Branch 6 (19→20) taken 2 times.
|
2 | fs << std::endl << "\t\t\"Get the size of the " << name << " in bytes\""; |
| 65 |
2/2✓ Branch 0 (20→21) taken 2 times.
✓ Branch 2 (21→22) taken 2 times.
|
2 | fs << std::endl << "\t\t)"; |
| 66 | 2 | } | |
| 67 | |||
| 68 | ///Registration of static method | ||
| 69 | /// @param fs stream to write to | ||
| 70 | /// @param classConfig config of the class | ||
| 71 | /// @param mode generator mode | ||
| 72 | 2 | void NanobindTraitDataStream::registerMethod(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{ | |
| 73 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 2 times.
|
2 | if(!mode.enableDataStream){return;} |
| 74 |
2/2✓ Branch 0 (4→5) taken 2 times.
✓ Branch 2 (5→6) taken 2 times.
|
2 | PString name(classConfig.getName()); |
| 75 | |||
| 76 |
1/1✓ Branch 0 (6→7) taken 2 times.
|
2 | registerSerializationMethods(fs, classConfig, mode); |
| 77 |
1/1✓ Branch 0 (7→8) taken 2 times.
|
2 | registerDeserializationMethods(fs, classConfig, mode); |
| 78 |
1/1✓ Branch 0 (8→9) taken 2 times.
|
2 | registerSizeMethod(fs, classConfig, mode); |
| 79 | 2 | } | |
| 80 | |||
| 81 | ///Generation of unit test function | ||
| 82 | /// @param fs stream to write to | ||
| 83 | /// @param classConfig config of the class | ||
| 84 | /// @param mode generator mode | ||
| 85 | /// @param baseFileName : base name of the configuration | ||
| 86 | 2 | void NanobindTraitDataStream::testFunction(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode, const PString & baseFileName) const{ | |
| 87 |
1/2✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 2 times.
|
2 | if(!mode.enableDataStream){return;} |
| 88 |
2/2✓ Branch 0 (4→5) taken 2 times.
✓ Branch 2 (5→6) taken 2 times.
|
2 | PString name(classConfig.getName()); |
| 89 |
3/3✓ Branch 0 (6→7) taken 2 times.
✓ Branch 2 (7→8) taken 2 times.
✓ Branch 4 (8→9) taken 2 times.
|
2 | PString moduleName = baseFileName.toLower() + "_module"; |
| 90 | ///Test base serialization/deserialization | ||
| 91 |
2/2✓ Branch 0 (11→12) taken 2 times.
✓ Branch 2 (12→13) taken 2 times.
|
2 | fs << "def test_data_stream_conversion():" << std::endl; |
| 92 |
2/2✓ Branch 0 (13→14) taken 2 times.
✓ Branch 2 (14→15) taken 2 times.
|
2 | fs << "\t\"\"\"Test data stream conversion\"\"\"" << std::endl; |
| 93 |
9/9✓ Branch 0 (15→16) taken 2 times.
✓ Branch 2 (16→17) taken 2 times.
✓ Branch 4 (17→18) taken 2 times.
✓ Branch 6 (18→19) taken 2 times.
✓ Branch 8 (19→20) taken 2 times.
✓ Branch 10 (20→21) taken 2 times.
✓ Branch 12 (21→22) taken 2 times.
✓ Branch 14 (22→23) taken 2 times.
✓ Branch 16 (23→24) taken 2 times.
|
2 | fs << "\t" << name.toLower() << " = " << moduleName << "." << name << "()" << std::endl; |
| 94 |
2/2✓ Branch 0 (25→26) taken 2 times.
✓ Branch 2 (26→27) taken 2 times.
|
2 | fs << "\t# Set some example values" << std::endl; |
| 95 |
1/1✓ Branch 0 (27→28) taken 2 times.
|
2 | const PVecClassAttribute & listAttr(classConfig.getListAttribute()); |
| 96 |
2/2✓ Branch 0 (56→29) taken 5 times.
✓ Branch 1 (56→57) taken 2 times.
|
14 | for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){ |
| 97 |
2/2✓ Branch 0 (31→32) taken 5 times.
✓ Branch 2 (32→33) taken 5 times.
|
5 | PString defaultValue = getTestDefaultValueTypeInPython(it->getType()); |
| 98 |
9/9✓ Branch 0 (33→34) taken 5 times.
✓ Branch 2 (34→35) taken 5 times.
✓ Branch 4 (35→36) taken 5 times.
✓ Branch 6 (36→37) taken 5 times.
✓ Branch 8 (39→40) taken 5 times.
✓ Branch 10 (40→41) taken 5 times.
✓ Branch 12 (41→42) taken 5 times.
✓ Branch 14 (42→43) taken 5 times.
✓ Branch 16 (43→44) taken 5 times.
|
10 | fs << "\t" << name.toLower() << "." << it->getName() << " = " << defaultValue << std::endl; |
| 99 | 5 | } | |
| 100 |
1/1✓ Branch 0 (57→58) taken 2 times.
|
2 | fs << std::endl; |
| 101 |
5/5✓ Branch 0 (58→59) taken 2 times.
✓ Branch 2 (59→60) taken 2 times.
✓ Branch 4 (60→61) taken 2 times.
✓ Branch 6 (61→62) taken 2 times.
✓ Branch 8 (62→63) taken 2 times.
|
2 | fs << "\tdata_stream = " << name.toLower() << ".to_bytes()" << std::endl; |
| 102 |
1/1✓ Branch 0 (64→65) taken 2 times.
|
2 | fs << std::endl; |
| 103 |
9/9✓ Branch 0 (65→66) taken 2 times.
✓ Branch 2 (66→67) taken 2 times.
✓ Branch 4 (67→68) taken 2 times.
✓ Branch 6 (68→69) taken 2 times.
✓ Branch 8 (69→70) taken 2 times.
✓ Branch 10 (70→71) taken 2 times.
✓ Branch 12 (71→72) taken 2 times.
✓ Branch 14 (72→73) taken 2 times.
✓ Branch 16 (73→74) taken 2 times.
|
2 | fs << "\tnew_" << name.toLower() << " = " << moduleName << "." << name << "()" << std::endl; |
| 104 |
5/5✓ Branch 0 (75→76) taken 2 times.
✓ Branch 2 (76→77) taken 2 times.
✓ Branch 4 (77→78) taken 2 times.
✓ Branch 6 (78→79) taken 2 times.
✓ Branch 8 (79→80) taken 2 times.
|
2 | fs << "\tsuccess = new_" << name.toLower() << ".from_bytes(data_stream)" << std::endl; |
| 105 |
1/1✓ Branch 0 (81→82) taken 2 times.
|
2 | fs << std::endl; |
| 106 |
2/2✓ Branch 0 (82→83) taken 2 times.
✓ Branch 2 (83→84) taken 2 times.
|
2 | fs << "\tassert success" << std::endl; |
| 107 |
2/2✓ Branch 0 (112→85) taken 5 times.
✓ Branch 1 (112→113) taken 2 times.
|
14 | for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){ |
| 108 |
2/2✓ Branch 0 (87→88) taken 5 times.
✓ Branch 2 (88→89) taken 5 times.
|
5 | PString defaultValue = getTestDefaultValueTypeInPython(it->getType()); |
| 109 |
9/9✓ Branch 0 (89→90) taken 5 times.
✓ Branch 2 (90→91) taken 5 times.
✓ Branch 4 (91→92) taken 5 times.
✓ Branch 6 (92→93) taken 5 times.
✓ Branch 8 (95→96) taken 5 times.
✓ Branch 10 (96→97) taken 5 times.
✓ Branch 12 (97→98) taken 5 times.
✓ Branch 14 (98→99) taken 5 times.
✓ Branch 16 (99→100) taken 5 times.
|
10 | fs << "\tassert new_" << name.toLower() << "." << it->getName() << " == " << defaultValue << std::endl; |
| 110 | 5 | } | |
| 111 | ///Test edge cases for serialization/deserialization | ||
| 112 |
1/1✓ Branch 0 (113→114) taken 2 times.
|
2 | fs << std::endl; |
| 113 |
2/2✓ Branch 0 (114→115) taken 2 times.
✓ Branch 2 (115→116) taken 2 times.
|
2 | fs << "def test_serialization_type_errors():" << std::endl; |
| 114 |
2/2✓ Branch 0 (116→117) taken 2 times.
✓ Branch 2 (117→118) taken 2 times.
|
2 | fs << "\t\"\"\"Test erro types for from_bytes\"\"\"" << std::endl; |
| 115 |
1/1✓ Branch 0 (118→119) taken 2 times.
|
2 | fs << std::endl; |
| 116 |
9/9✓ Branch 0 (119→120) taken 2 times.
✓ Branch 2 (120→121) taken 2 times.
✓ Branch 4 (121→122) taken 2 times.
✓ Branch 6 (122→123) taken 2 times.
✓ Branch 8 (123→124) taken 2 times.
✓ Branch 10 (124→125) taken 2 times.
✓ Branch 12 (125→126) taken 2 times.
✓ Branch 14 (126→127) taken 2 times.
✓ Branch 16 (127→128) taken 2 times.
|
2 | fs << "\t" << name.toLower() << " = " << moduleName << "." << name << "()" << std::endl; |
| 117 |
2/2✓ Branch 0 (129→130) taken 2 times.
✓ Branch 2 (130→131) taken 2 times.
|
2 | fs << "\ttry:" << std::endl; |
| 118 |
5/5✓ Branch 0 (131→132) taken 2 times.
✓ Branch 2 (132→133) taken 2 times.
✓ Branch 4 (133→134) taken 2 times.
✓ Branch 6 (134→135) taken 2 times.
✓ Branch 8 (135→136) taken 2 times.
|
2 | fs << "\t\tresult = " << name.toLower() << ".from_bytes(\"A string is not a bytearray !!!\") # deserialize string instead of bytearray" << std::endl; |
| 119 |
2/2✓ Branch 0 (137→138) taken 2 times.
✓ Branch 2 (138→139) taken 2 times.
|
2 | fs << "\t\tassert False" << std::endl; |
| 120 |
2/2✓ Branch 0 (139→140) taken 2 times.
✓ Branch 2 (140→141) taken 2 times.
|
2 | fs << "\texcept TypeError:" << std::endl; |
| 121 |
2/2✓ Branch 0 (141→142) taken 2 times.
✓ Branch 2 (142→143) taken 2 times.
|
2 | fs << "\t\tpass" << std::endl; |
| 122 |
1/1✓ Branch 0 (143→144) taken 2 times.
|
2 | fs << std::endl; |
| 123 |
2/2✓ Branch 0 (144→145) taken 2 times.
✓ Branch 2 (145→146) taken 2 times.
|
2 | fs << "\ttry:" << std::endl; |
| 124 |
5/5✓ Branch 0 (146→147) taken 2 times.
✓ Branch 2 (147→148) taken 2 times.
✓ Branch 4 (148→149) taken 2 times.
✓ Branch 6 (149→150) taken 2 times.
✓ Branch 8 (150→151) taken 2 times.
|
2 | fs << "\t\tresult = " << name.toLower() << ".from_bytes(42) # deserialize instead of bytearray" << std::endl; |
| 125 |
2/2✓ Branch 0 (152→153) taken 2 times.
✓ Branch 2 (153→154) taken 2 times.
|
2 | fs << "\t\tassert False" << std::endl; |
| 126 |
2/2✓ Branch 0 (154→155) taken 2 times.
✓ Branch 2 (155→156) taken 2 times.
|
2 | fs << "\texcept TypeError:" << std::endl; |
| 127 |
2/2✓ Branch 0 (156→157) taken 2 times.
✓ Branch 2 (157→158) taken 2 times.
|
2 | fs << "\t\tpass" << std::endl; |
| 128 |
1/1✓ Branch 0 (158→159) taken 2 times.
|
2 | fs << std::endl; |
| 129 |
2/2✓ Branch 0 (159→160) taken 2 times.
✓ Branch 2 (160→161) taken 2 times.
|
2 | fs << "\ttry:" << std::endl; |
| 130 |
5/5✓ Branch 0 (161→162) taken 2 times.
✓ Branch 2 (162→163) taken 2 times.
✓ Branch 4 (163→164) taken 2 times.
✓ Branch 6 (164→165) taken 2 times.
✓ Branch 8 (165→166) taken 2 times.
|
2 | fs << "\t\tresult = " << name.toLower() << ".from_bytes([1, 2, 3, 4]) # deserialize list instead of bytearray" << std::endl; |
| 131 |
2/2✓ Branch 0 (167→168) taken 2 times.
✓ Branch 2 (168→169) taken 2 times.
|
2 | fs << "\t\tassert False" << std::endl; |
| 132 |
2/2✓ Branch 0 (169→170) taken 2 times.
✓ Branch 2 (170→171) taken 2 times.
|
2 | fs << "\texcept TypeError:" << std::endl; |
| 133 |
3/3✓ Branch 0 (171→172) taken 2 times.
✓ Branch 2 (172→173) taken 2 times.
✓ Branch 4 (173→174) taken 2 times.
|
2 | fs << "\t\tpass" << std::endl << std::endl; |
| 134 | 2 | } | |
| 135 | |||
| 136 | ///Generation of unit test call function | ||
| 137 | /// @param fs stream to write to | ||
| 138 | /// @param classConfig config of the class | ||
| 139 | /// @param mode generator mode | ||
| 140 | ✗ | void NanobindTraitDataStream::testCallFunction(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{ | |
| 141 | ✗ | if(!mode.enableDataStream){return;} | |
| 142 | ✗ | PString name(classConfig.getName()); | |
| 143 | ✗ | fs << "\ttest_data_stream_conversion()" << std::endl; | |
| 144 | ✗ | fs << "\ttest_serialization_type_errors()" << std::endl; | |
| 145 | ✗ | } | |
| 146 | |||
| 147 |