-
Notifications
You must be signed in to change notification settings - Fork 0
Char Stream Request - Optimized for high rate #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
EdDev
wants to merge
1
commit into
master
Choose a base branch
from
high_rate_char_stream_request
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * char_stream_req.cpp | ||
| * | ||
| * Created on: Nov 14, 2014 | ||
| * Author: edwardh | ||
| */ | ||
|
|
||
| #include <cstring> | ||
|
|
||
| #include "char_stream_req.h" | ||
|
|
||
|
|
||
| CharStreamReq::CharStreamReq() | ||
| { | ||
| this->max_num_of_unique_chars = (unsigned char)~0 + 1; | ||
| this->char_occurrence_count = new unsigned int[max_num_of_unique_chars]; | ||
|
|
||
| clearRecv(); | ||
| } | ||
|
|
||
| CharStreamReq::~CharStreamReq() | ||
| { | ||
| delete []this->char_occurrence_count; | ||
| } | ||
|
|
||
|
|
||
| void CharStreamReq::recv(unsigned char in_char) | ||
| { | ||
| char_occurrence_count[in_char]++; | ||
| } | ||
|
|
||
|
|
||
| void CharStreamReq::printRecvSorted(char *outbuffer, size_t buf_size) | ||
| { | ||
| size_t char_out_itr = 0; | ||
|
|
||
| for(unsigned int char_in_itr = 0; char_in_itr < max_num_of_unique_chars; ++char_in_itr) | ||
| { | ||
| int num_of_chars_to_output = char_occurrence_count[char_in_itr]; | ||
| for(int duplicate_char_out_itr = 0; | ||
| (duplicate_char_out_itr < num_of_chars_to_output) && (char_out_itr < buf_size-1); | ||
| ++duplicate_char_out_itr) | ||
| { | ||
| outbuffer[char_out_itr] = char_in_itr; | ||
| char_out_itr++; | ||
| } | ||
| } | ||
|
|
||
| outbuffer[char_out_itr] = '\0'; | ||
| } | ||
|
|
||
| void CharStreamReq::clearRecv() | ||
| { | ||
| std::memset(this->char_occurrence_count, 0 ,max_num_of_unique_chars * sizeof(*char_occurrence_count)); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * char_stream_req.h | ||
| * | ||
| * Description: | ||
| * Receive a stream of characters (ASCII), accumulate them and | ||
| * provide the means to output them in an acceding sorted manner. | ||
| * | ||
| * Limitations: | ||
| * - Sorted based on the ASCII table, no special care has been done to handle upper and lower characters. | ||
| * - It can accumulate between 2^(sizeof(int)) to 256 * (2^(sizeof(int))) characters. | ||
| * | ||
| * Note: It is up to the application that uses this module to specify how many characters to recv | ||
| * until a print is performed, followed by a clear to reset the data. | ||
| * | ||
| * | ||
| * Created on: Nov 14, 2014 | ||
| * Author: edwardh | ||
| */ | ||
|
|
||
| #ifndef CHAR_STREAM_REQ_H_ | ||
| #define CHAR_STREAM_REQ_H_ | ||
|
|
||
|
|
||
| class CharStreamReq | ||
| { | ||
| public: | ||
| CharStreamReq(); | ||
| ~CharStreamReq(); | ||
|
|
||
| void recv(unsigned char in_char); | ||
| void printRecvSorted(char *outbuffer, size_t buf_size); | ||
| void clearRecv(); | ||
|
|
||
| private: | ||
| unsigned int *char_occurrence_count; | ||
| unsigned int max_num_of_unique_chars; | ||
| }; | ||
|
|
||
|
|
||
| #endif /* CHAR_STREAM_REQ_H_ */ |
32 changes: 17 additions & 15 deletions
32
unit_tester/build/mk_common/utest_config_production_path.mk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,18 @@ | ||
| # | ||
| # | ||
| # | ||
|
|
||
| # When all source files in a folder are under tests, it is prefered to add the folder instead of adding individual source files. | ||
| SRC_DIRS = \ | ||
| $(PRODUCTION_SOURCES)/llist | ||
| # <Add here the folders that contain code under test> | ||
|
|
||
| SRC_FILES = \ | ||
| # $(PRODUCTION_SOURCES)/source_file.cpp | ||
| # <Add here the source file that is being tested> | ||
|
|
||
| INCLUDE_DIRS +=\ | ||
| $(PRODUCTION_SOURCES)/ | ||
| # | ||
| # | ||
| # | ||
|
|
||
| # When all source files in a folder are under tests, it is prefered to add the folder instead of adding individual source files. | ||
| SRC_DIRS = \ | ||
| $(PRODUCTION_SOURCES)/llist\ | ||
| $(PRODUCTION_SOURCES)/char_stream_req\ | ||
| # <Add here the folders that contain code under test> | ||
|
|
||
| SRC_FILES = \ | ||
| # $(PRODUCTION_SOURCES)/source_file.cpp | ||
| # <Add here the source file that is being tested> | ||
|
|
||
| INCLUDE_DIRS +=\ | ||
| $(PRODUCTION_SOURCES)/\ | ||
| $(PRODUCTION_SOURCES)/char_stream_req\ | ||
| # <Add here the folder that contains the headers. Note that the order is important!> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,20 @@ | ||
| # | ||
| # | ||
| # | ||
|
|
||
| INCLUDE_DIRS =\ | ||
| .\ | ||
| $(TEST_ROOT)/helpers/include\ | ||
| $(TEST_ROOT)/mocks/include\ | ||
| /usr/include\ | ||
| $(CPPUTEST_HOME)/include\ | ||
|
|
||
| TEST_SRC_DIRS = \ | ||
| $(TEST_ROOT)/helpers/src\ | ||
| $(TEST_ROOT)/tests\ | ||
| $(TEST_ROOT)/tests/llist_test\ | ||
| # <Add here the folder that contains the tester source> | ||
|
|
||
| MOCKS_SRC_DIRS =\ | ||
| # | ||
| # | ||
| # | ||
|
|
||
| INCLUDE_DIRS =\ | ||
| .\ | ||
| $(TEST_ROOT)/helpers/include\ | ||
| $(TEST_ROOT)/mocks/include\ | ||
| /usr/include\ | ||
| $(CPPUTEST_HOME)/include\ | ||
|
|
||
| TEST_SRC_DIRS = \ | ||
| $(TEST_ROOT)/helpers/src\ | ||
| $(TEST_ROOT)/tests\ | ||
| $(TEST_ROOT)/tests/llist_test\ | ||
| $(TEST_ROOT)/tests/char_stream_req_test\ | ||
| # <Add here the folder that contains the tester source> | ||
|
|
||
| MOCKS_SRC_DIRS =\ | ||
| $(TEST_ROOT)/mocks/src\ |
98 changes: 98 additions & 0 deletions
98
unit_tester/tests/char_stream_req_test/char_stream_req_test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| * char_stream_req_test.cpp | ||
| * | ||
| * Created on: Nov 14, 2014 | ||
| * Author: edwardh | ||
| */ | ||
|
|
||
| #include "CppUTest/TestHarness.h" | ||
|
|
||
| #include "char_stream_req.h" | ||
|
|
||
|
|
||
| TEST_GROUP(char_stream_req) | ||
| { | ||
| static const int bulk_size = 100000; | ||
|
|
||
| void setup() | ||
| { | ||
| } | ||
|
|
||
| void teardown() | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| TEST(char_stream_req, unique_input_alphanumeric) | ||
| { | ||
|
|
||
| const char *cstream_in = "abc123"; | ||
| const char *cstream_expected = "123abc"; | ||
| char cstream_out[bulk_size]; | ||
|
|
||
| CharStreamReq csr; | ||
|
|
||
| for(char *ch_itr = (char*)cstream_in; (*ch_itr != '\0'); ++ch_itr) | ||
| csr.recv(*ch_itr); | ||
|
|
||
| csr.printRecvSorted(cstream_out, bulk_size); | ||
|
|
||
| STRCMP_EQUAL(cstream_expected, cstream_out); | ||
| } | ||
|
|
||
| TEST(char_stream_req, non_unique_input_alphanumeric) | ||
| { | ||
|
|
||
| const char *cstream_in = "abbc13b213"; | ||
| const char *cstream_expected = "11233abbbc"; | ||
| char cstream_out[bulk_size]; | ||
|
|
||
| CharStreamReq csr; | ||
|
|
||
| for(char *ch_itr = (char*)cstream_in; (*ch_itr != '\0'); ++ch_itr) | ||
| csr.recv(*ch_itr); | ||
|
|
||
| csr.printRecvSorted(cstream_out, bulk_size); | ||
|
|
||
| STRCMP_EQUAL(cstream_expected, cstream_out); | ||
| } | ||
|
|
||
| /* | ||
| * Current implementation assumes that the output is sorted based on the | ||
| * ASCII table (For alphanumeric: Numbers, Upper chars and lastly Lower chars. | ||
| */ | ||
| TEST(char_stream_req, non_unique_input_alphanumeric_w_low_and_up_chars) | ||
| { | ||
|
|
||
| const char *cstream_in = "abBc13Ab213"; | ||
| const char *cstream_expected = "11233ABabbc"; | ||
| char cstream_out[bulk_size]; | ||
|
|
||
| CharStreamReq csr; | ||
|
|
||
| for(char *ch_itr = (char*)cstream_in; (*ch_itr != '\0'); ++ch_itr) | ||
| csr.recv(*ch_itr); | ||
|
|
||
| csr.printRecvSorted(cstream_out, bulk_size); | ||
|
|
||
| STRCMP_EQUAL(cstream_expected, cstream_out); | ||
| } | ||
|
|
||
| TEST(char_stream_req, input_larger_than_outbuffer__output_only_4_chars) | ||
| { | ||
|
|
||
| const char *cstream_in = "a1b3c2"; | ||
| const char *cstream_expected = "123"; | ||
|
|
||
| const int small_bulk_size = 4; | ||
| char cstream_out[small_bulk_size]; | ||
|
|
||
| CharStreamReq csr; | ||
|
|
||
| for(char *ch_itr = (char*)cstream_in; (*ch_itr != '\0'); ++ch_itr) | ||
| csr.recv(*ch_itr); | ||
|
|
||
| csr.printRecvSorted(cstream_out, small_bulk_size); | ||
|
|
||
| STRCMP_EQUAL(cstream_expected, cstream_out); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is constant, therefore can be a member of the class without the need to use new/delete in the const/destr.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use std::numeric_limits::max() instead of this trick.
(From #include )