mirror of
https://github.com/firebase/nanopb
synced 2026-11-09 22:25:59 +05:45
Unsorted enums in message specifications do not result in correct min/max values per the protobuf spec for cpp code: https://developers.google.com/protocol-buffers/docs/reference/cpp-generated#enum The google cpp compiler uses the min/max _value_ instead of the position of the enum entry in the message spec, so this patch updates nanopb to do the same. Example input: ```protobuf syntax = "proto3"; enum Language { UNKNOWN = 0; ENGLISH_EN_GB = 12; ENGLISH_EN_US = 1; FRENCH_FR_FR = 2; ITALIAN_IT_IT = 3; GERMAN_DE_DE = 4; SPANISH_ES_AR = 13; SPANISH_ES_ES = 5; SPANISH_ES_MX = 14; SWEDISH_SV_SE = 6; DUTCH_NL_NL = 7; KOREAN_KO_KR = 8; JAPANESE_JA_JP = 9; CHINESE_SIMPLIFIED_ZH_CN = 10; CHINESE_TRADITIONAL_ZH_TW = 11; } ``` This would previously result in: ```c #define _Language_MIN Language_UNKNOWN #define _Language_MAX Language_CHINESE_TRADITIONAL_ZH_TW #define _Language_ARRAYSIZE ((Language)(Language_CHINESE_TRADITIONAL_ZH_TW+1)) ``` After this patch, ```c #define _Language_MIN Language_UNKNOWN #define _Language_MAX Language_SPANISH_ES_MX #define _Language_ARRAYSIZE ((Language)(Language_SPANISH_ES_MX+1)) ``` Add a test `enum_minmax` to cover this case.
17 lines
336 B
C
17 lines
336 B
C
#include "unittests.h"
|
|
#include "enumminmax.pb.h"
|
|
|
|
int main()
|
|
{
|
|
int status = 0;
|
|
|
|
COMMENT("Verify min/max on unsorted enum");
|
|
{
|
|
TEST(_Language_MIN == Language_UNKNOWN);
|
|
TEST(_Language_MAX == Language_SPANISH_ES_MX);
|
|
TEST(_Language_ARRAYSIZE == (Language_SPANISH_ES_MX+1));
|
|
}
|
|
|
|
return status;
|
|
}
|