I have this in my code,
template<
typename Ptr = byte*,
typename Diff = typename std::pointer_traits<Ptr>::difference_type
>
struct block;
How do I prevent clang-format to reflow this as:
template<
typename Ptr = byte*,
typename Diff = typename std::pointer_traits<Ptr>::difference_type>
struct block;
This is my configuration file so far:
---
Language: Cpp
AlignAfterOpenBracket: BlockIndent # Align
AlwaysBreakTemplateDeclarations: No # Yes
ColumnLimit: 0
FixNamespaceComments: true
AccessModifierOffset: -99 # 2
IndentWidth: 100
PenaltyBreakTemplateDeclaration: 10
PointerAlignment: Left
SpaceAfterTemplateKeyword: false # true
TabWidth: 100
UseTab: Always # Never
...
I am using clang-format 14.0.0.
Related
I have a dataframe dat with data and a vector rule with logical rules
set.seed(124)
ro <- round(runif(n = 30,1,10),2)
dat <- as.data.frame(matrix(data =ro,ncol = 3)) ; colnames(dat) <- paste0("x" ,1:ncol(dat))
rule <- c("x1 > 5 & x2/2 > 2" , "x1 > x2*2" , "x3!=4")
I need to check if the expression is true
id <- 2
for(i in 1:nrow(dat)){
cr <- with(data = dat[i,] , expr = eval(parse(text = rule[id])))
print(cr)
}
[1] FALSE
[1] FALSE
[1] FALSE
[1] FALSE
[1] FALSE
[1] TRUE
[1] FALSE
[1] FALSE
[1] FALSE
[1] TRUE
How to do this with Rcpp ?
Two things worth stressing here are
you do not need a low over all rows as R is vectorized, and that already fast
you can sweep the rules over your data and return a result matrix
Both of those are a one-liner:
> res <- do.call(cbind, lapply(rule, \(r) with(dat, eval(parse(text=r)))))
> res
[,1] [,2] [,3]
[1,] FALSE FALSE TRUE
[2,] FALSE FALSE TRUE
[3,] TRUE FALSE TRUE
[4,] FALSE FALSE TRUE
[5,] FALSE FALSE TRUE
[6,] FALSE TRUE TRUE
[7,] TRUE FALSE TRUE
[8,] TRUE FALSE TRUE
[9,] TRUE FALSE TRUE
[10,] FALSE TRUE TRUE
>
(I used the R 4.1.* anonymous function there, you can relace \(r) with the standard function(r) as well.)
As this is already vectorised it will be faster than your per-row call, and even if you did it with Rcpp if would not be (much) faster than already vectorised code.
I'm here because honestly, I can't figure out this one.
My problem lies in how clang-format rearranges long preprocessor definition.
Here's what my code look like, after formating.
#define CMD_FRAME_PACKET_HEADER_SIZE \
(CMD_FRAME_SOF_SIZE + CMD_FRAME_MSG_ID_SIZE + CMD_FRAME_CMD_CODE_SIZE + CMD_FRAME_CMD_STATE_SIZE + \
CMD_FRAME_DATA_LENGTH_SIZE)
#define CMD_FRAME_PACKET_FOOTER_SIZE (CMD_FRAME_CRC_SIZE + CMD_FRAME_EOF_SIZE)
#define CMD_FRAME_PACKET_PAYLOAD_MAX_SIZE (2 * CMD_FRAME_DATA_FIELDS_MAX_SIZE)
#define CMD_FRAME_PACKET_PAYLOAD_MIN_SIZE (0)
#define CMD_FRAME_PACKET_MAX_SIZE \
(CMD_FRAME_PACKET_HEADER_SIZE + CMD_FRAME_PACKET_PAYLOAD_MAX_SIZE + CMD_FRAME_PACKET_FOOTER_SIZE)
#define CMD_FRAME_PACKET_MIN_SIZE \
(CMD_FRAME_PACKET_HEADER_SIZE + CMD_FRAME_PACKET_PAYLOAD_MIN_SIZE + CMD_FRAME_PACKET_FOOTER_SIZE)
Here's what I like to see:
#define CMD_FRAME_PACKET_HEADER_SIZE (CMD_FRAME_SOF_SIZE + CMD_FRAME_MSG_ID_SIZE + CMD_FRAME_CMD_CODE_SIZE \
+ CMD_FRAME_CMD_STATE_SIZE + CMD_FRAME_DATA_LENGTH_SIZE)
#define CMD_FRAME_PACKET_FOOTER_SIZE (CMD_FRAME_CRC_SIZE + CMD_FRAME_EOF_SIZE)
#define CMD_FRAME_PACKET_PAYLOAD_MAX_SIZE (2 * CMD_FRAME_DATA_FIELDS_MAX_SIZE)
#define CMD_FRAME_PACKET_PAYLOAD_MIN_SIZE (0)
#define CMD_FRAME_PACKET_MAX_SIZE (CMD_FRAME_PACKET_HEADER_SIZE + CMD_FRAME_PACKET_PAYLOAD_MAX_SIZE \
+ CMD_FRAME_PACKET_FOOTER_SIZE)
#define CMD_FRAME_PACKET_MIN_SIZE (CMD_FRAME_PACKET_HEADER_SIZE + CMD_FRAME_PACKET_PAYLOAD_MIN_SIZE \
+ CMD_FRAME_PACKET_FOOTER_SIZE)
For those interested, here is my .clang-format
---
#
# Global style
BasedOnStyle: Microsoft
#
# Basic
IndentWidth: 4
TabWidth: 4
ColumnLimit: 120
UseTab: Never
#
# Function and macro
AlignAfterOpenBracket: Align
AllowAllParametersOfDeclarationOnNextLine: false
BinPackParameters: true
AlignConsecutiveMacros: Consecutive
AllowAllArgumentsOnNextLine: false
#
# if and switch case
AllowShortCaseLabelsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
AlignTrailingComments: true
IndentCaseLabels: true
BraceWrapping:
AfterCaseLabel: true
#
# Other
SpaceInEmptyBlock: true
AlignConsecutiveAssignments: Consecutive
SpacesInContainerLiterals: true
# AlignArrayOfStructures: Right
"what I like to see" involves the layout of one macro to depend on others. Should that "other" macros only depend on nearby macros, all macros in the .c file, all macros in the .c and .h files or what?
Consider how one long macro would impact the others
#define CMD_FRAME_PACKET_FOOTER_SIZE_AALKJSLFAKJFALFEUEUFELUFEU (0)
#define CMD_FRAME_PACKET_HEADER_SIZE (CMD_FRAME_SOF_SIZE \
+ CMD_FRAME_MSG_ID_SIZE \
+ CMD_FRAME_CMD_CODE_SIZE \
+ CMD_FRAME_CMD_STATE_SIZE \
+ CMD_FRAME_DATA_LENGTH_SIZE)
The desired format is higher maintenance and not effective use of valuable coder time. OTOH, your own time is yours to do as you will. Yet since much programming is paid by others, payers would not likely want to pay for this and so Clang (or other simple auto formatting) becomes more common and familiar/understandable.
I recommend to format code based on your group's coding standard.
Let's say that I have a list std::list<MyClass> myList like this:
ID Valid
--------------
1000 true
1000 false
1000 true
2000 false
2000 false
3000 true
3000 true
And this boolean value comes from a function bool isExpired(MyClass &a).
How can I check if one group of elements have equal or different boolean values?
For example in this case 1000 should be false because second 1000 in list has false value.
This is false
1000 true
1000 false //because of this
1000 true
This is false
2000 false
2000 false
This is true
3000 true
3000 true
I tried to create a new map which will override the key and value.
std::map<long, bool> status;
for(const auto &it : myList)
{
status[it.ID] = status[it.ID] || isExpired(it);
}
But it does not work as expected. It returns true for the element with an ID 1000.
You don't want to use ||, you want to use &&, which means that you have to default to true:
std::map<long, bool> status;
for(const auto &it : myList)
{
auto entry = status.find(it.ID);
if (entry != status.end()) {
entry->second = entry->second && isExpired(it);
} else {
status[it.ID] = true;
}
}
I applied GeoSeries.almost_equals(other[, decimal=6]) function to geo data frame with 10 mil entries, in order to find multiple geo points close to each other.
:
which gave me matrix, now i need to filter all True values in order to create DF/list with only POI that are geo related, so I used:
Now, I struggle to figure out how to proceed further with filters of this matrix.
Expected output is either vector, list or ideally DF with all TRUE (matched) values but matched to each other re 1 to 1, and repeated (if [1,9] then [9,1] to be removed from output
list example:
DF example:
Consider this example dataframe:
In [1]: df = pd.DataFrame([[True, False, False, True],
...: [False, True, True, False],
...: [False, True, True, False],
...: [True, False, False, True]])
In [2]: df
Out[2]:
0 1 2 3
0 True False False True
1 False True True False
2 False True True False
3 True False False True
A possible solution to get to the dataframe of matching indexes:
First I use np.triu to only consider the upper triangle (so you don't have duplicates):
In [15]: df2 = pd.DataFrame(np.triu(df))
In [16]: df2
Out[16]:
0 1 2 3
0 True False False True
1 False True True False
2 False False True False
3 False False False True
Then I stack the dataframe, give the index levels the desired names, and select only the rows where we have 'True' values:
In [17]: result = df2.stack()
In [18]: result
Out[18]:
0 0 True
1 False
2 False
3 True
1 0 False
1 True
2 True
3 False
2 0 False
1 False
2 True
3 False
3 0 False
1 False
2 False
3 True
dtype: bool
In [21]: result.index.names = ['POI_id', 'matched_POI_ids']
In [23]: result[result].reset_index()
Out[23]:
POI_id matched_POI_ids 0
0 0 0 True
1 0 3 True
2 1 1 True
3 1 2 True
4 2 2 True
5 3 3 True
You can then of course delete the column with trues: .drop(0, axis=1)
So I have this reaaaally long method that I want to test. I could probably break it some other 4-5 methods, but I don't want to make those methods public (and although I could, I'd like to stay away, if possible, from turning them into package-protected/internal):
If my math isn't wrong, to test this as it is I'll need something like 8 tests. Dividing this in smaller methods wouldn't make me have to make less unit-tests, would it?
The big problem I see with creating smaller methods(for testing) besides the visility issue is the need to pass to those small methods lots of arguments.
Although it was me who originally wrote this code, I still find it hard to understand.
So my question is...how to test this?!? How would you do it? Should I restructure this in any way?
public Genoma GenerateOffspring(Genoma genoma1, Genoma genoma2) {
if (genoma1.NumberOfGenes != genoma2.NumberOfGenes) throw new ArgumentException("Both individuals must have the same size!");
if (genoma1.NumberOfGenes == 0 || genoma2.NumberOfGenes == 0) throw new ArgumentException("0-sized genomas not allowed!");
if (genoma1.NumberOfGenes == 1) {
if (numberGenerator.GenerateInteger(0, 2) == 0) {
return genoma1;
} else {
return genoma2;
}
}
int cutPoint = numberGenerator.GenerateInteger(1, genoma1.NumberOfGenes);
Genoma parent1;
Genoma parent2;
if (numberGenerator.GenerateBool()) {
parent1 = genoma1;
parent2 = genoma2;
} else {
parent1 = genoma2;
parent2 = genoma1;
}
Genoma offspring = parent1.Clone();
if (numberGenerator.GenerateBool()) {
for (int i = 0; i < cutPoint; ++i) {
offspring.SetGeneAt(i, parent2.GetGeneAt(i));
}
} else {
for (int i = cutPoint; i < offspring.NumberOfGenes; ++i) {
offspring.SetGeneAt(i, parent2.GetGeneAt(i));
}
}
return offspring;
}
Thanks
The main thing to keep in mind is, when you start testing, you will run all tests, but for a given test, if it fails one assertion at some point, it will stop, meaning that what is after it will not be tested because it is assumed to require what was already not correctly asserted.
The good thing about dividing it into smaller methods would be that you would end up getting more things that could be tested independently, but the increase in arguments that you mention suggests that it would not be a "natural" thing to do, so you should keep it all in same test because you are, according to your words, seeing the whole function as a unit itself, and so, one unit test should test that unit.
Additionally, the increase in arguments would also make your code less pretty, and less efficient, I think.
I hope that helps.
I say there is no easy way around this one. You seem to be generating some values randomly then deciding how to set you instance variables so something like a truth table basically might help you there, in order to check for all possible values on the variables you are setting. Also I would test the if statements at the top in order to make sure that those conditions are satisfied before you proceed to the rest. There is now way to test easily, only thoroughly.
Skimming over your code, you should at least test following conditions
genoma1.NumberofGenes = genoma2.NumberOfGenes
genoma1.NumberofGenes <> genoma2.NumberOfGenes
genoma1.NumberofGenes = -1
genoma1.NumberofGenes = 0
genoma1.NumberofGenes = 1
genoma2.NumberofGenes = -1
genoma2.NumberofGenes = 0
genoma2.NumberofGenes = 1
genoma1.NumberOfGenes = HighBound - 1
genoma1.NumberOfGenes = HighBound (Max Integer?)
genoma1.NumberOfGenes = HighBound + 1
genoma2.NumberOfGenes = HighBound - 1
genoma2.NumberOfGenes = HighBound (Max Integer?)
genoma2.NumberOfGenes = HighBound + 1
Your testing effort could greatly benefit from mocking the numberGenerator object and have it return a fixed value.
This would give you following additional conditions to be tested
numberGenerator.GenerateInteger = LowBound - 1
numberGenerator.GenerateInteger = LowBound
numberGenerator.GenerateInteger = LowBound + 1
numberGenerator.GenerateInteger = HighBound - 1
numberGenerator.GenerateInteger = HighBound
numberGenerator.GenerateInteger = HighBound + 1
numberGenerator.Generate = True
numberGenerator.GenerateBool = False
Testing ALL possible combinations with given intputs calls for 432 testcases (6.6.6.2)
Using a pairwise testgeneration tool reduces this to 39 testcases.
When all your tests pass, you should run a coverage profiler to verify you haven't missed a coding path. Every path should get executed at least once.
By now, you have code that has most of the bugs removed.
By now, you have code that most likely still contains some bugs. (sad but true).
PICT Model
genoma1_NumberOfGenes: -1, 0, 1, HighboundMinOne, HighBound, HighBoundPlusOne
genoma2_NumberOfGenes: -1, 0, 1, HighboundMinOne, HighBound, HighBoundPlusOne
numberGenerator.GenerateInteger: LowBoundMinOne, LowBound, LowBoundPlusOne, HighBoundMinOne, HighBound, HighBoundPlusOne
numberGenerator.GenerateBool: True, False
PICT Generated testcase parameters
genoma1_NumberOfGenes genoma2_NumberOfGenes numberGenerator.GenerateInteger numberGenerator.GenerateBool
HighBoundPlusOne -1 HighBoundPlusOne True
HighBound 1 HighBoundPlusOne False
-1 HighBoundPlusOne HighBound True
HighBoundPlusOne 0 LowBound False
0 HighBoundPlusOne HighBoundPlusOne False
0 1 HighBoundMinOne True
-1 0 HighBoundMinOne False
HighBound 0 LowBoundPlusOne True
1 HighboundMinOne LowBound True
HighboundMinOne HighBound LowBoundMinOne False
-1 HighBound HighBoundPlusOne True
1 0 HighBound False
HighBoundPlusOne HighboundMinOne HighBoundMinOne False
HighboundMinOne HighboundMinOne LowBoundMinOne True
HighBound -1 LowBoundMinOne False
HighBoundPlusOne 1 LowBoundMinOne True
HighBoundPlusOne HighBound LowBoundPlusOne False
1 -1 HighBoundMinOne True
HighBound HighBound HighBoundMinOne False
1 HighboundMinOne HighBoundPlusOne True
HighBound HighboundMinOne HighBound False
1 1 LowBoundPlusOne False
HighBoundPlusOne -1 HighBound True
0 0 LowBoundMinOne True
1 HighBound LowBound True
0 HighBound HighBound False
HighBound HighBoundPlusOne LowBound True
-1 -1 LowBoundMinOne True
0 HighboundMinOne LowBoundPlusOne True
HighBoundPlusOne HighBoundPlusOne LowBoundPlusOne False
HighboundMinOne 1 HighBound True
-1 1 LowBound True
HighboundMinOne -1 LowBound True
HighboundMinOne -1 LowBoundPlusOne False
-1 HighboundMinOne LowBoundPlusOne False
HighboundMinOne HighBoundPlusOne HighBoundMinOne False
HighboundMinOne 0 HighBoundPlusOne False
1 HighBoundPlusOne LowBoundMinOne False
0 -1 LowBound False