forked from UnitTestBot/unittestbot.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent---src-docs-advanced-c-syntax-md-1b5ca7527bbcd51a9451.js
2 lines (2 loc) · 41.3 KB
/
component---src-docs-advanced-c-syntax-md-1b5ca7527bbcd51a9451.js
1
2
"use strict";(self.webpackChunkunittestbot_web=self.webpackChunkunittestbot_web||[]).push([[284],{91902:function(n,t,e){e.r(t),e.d(t,{_frontmatter:function(){return o},default:function(){return l}});var r=e(87462),a=e(63366),i=(e(15007),e(64983)),s=e(23017),c=(e(8156),["components"]),o={};void 0!==o&&o&&o===Object(o)&&Object.isExtensible(o)&&!Object.prototype.hasOwnProperty.call(o,"__filemeta")&&Object.defineProperty(o,"__filemeta",{configurable:!0,value:{name:"_frontmatter",filename:"src/docs/advanced/c-syntax.md"}});var u={_frontmatter:o},p=s.Z;function l(n){var t=n.components,e=(0,a.Z)(n,c);return(0,i.kt)(p,(0,r.Z)({},u,e,{components:t,mdxType:"MDXLayout"}),(0,i.kt)("h1",{id:"supported-c-syntax"},"Supported C Syntax"),(0,i.kt)("p",null,"UnitTestBot supports the majority of C language features. Here you can find test cases examples on main syntax\nconstructions. All code snippets below were taken\nfrom ",(0,i.kt)("a",{parentName:"p",href:"https://github.com/UnitTestBot/UTBotCpp/tree/main/integration-tests/c-example/"},"this directory"),"."),(0,i.kt)("ul",null,(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("a",{parentName:"li",href:"#integral-types"},"Integral types")),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("a",{parentName:"li",href:"#character-types"},"Character types")),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("a",{parentName:"li",href:"#floating-point-types"},"Floating-point types")),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("a",{parentName:"li",href:"#_bool-type"},"_Bool type")),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("a",{parentName:"li",href:"#structs-as-parameters"},"Structs as parameters")),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("a",{parentName:"li",href:"#structs-as-return-values"},"Structs as return values")),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("a",{parentName:"li",href:"#unions"},"Unions")),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("a",{parentName:"li",href:"#pointers-as-parameters-and-return-types"},"Pointers as parameters and return types")),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("a",{parentName:"li",href:"#pointers-as-struct-fields-members"},"Pointers as struct fields members")),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("a",{parentName:"li",href:"#pointers-to-functions"},"Pointers to functions")),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("a",{parentName:"li",href:"#arrays"},"Arrays")),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("a",{parentName:"li",href:"#multidimensional-arrays-and-pointers"},"Multidimensional arrays and pointers")),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("a",{parentName:"li",href:"#enums"},"Enums")),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("a",{parentName:"li",href:"#typedef"},"Typedef")),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("a",{parentName:"li",href:"#static-functions"},"Static functions")),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("a",{parentName:"li",href:"#qualifiers-const-volatile-restrict-etc"},"Qualifiers: const, volatile, restrict etc.")),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("a",{parentName:"li",href:"#global-variables"},"Global variables"))),(0,i.kt)("h2",{id:"integral-types"},"Integral types"),(0,i.kt)("blockquote",null,(0,i.kt)("p",{parentName:"blockquote"},(0,i.kt)("inlineCode",{parentName:"p"},"short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long, unsigned char"))),(0,i.kt)("p",null,(0,i.kt)("a",{parentName:"p",href:"https://github.com/UnitTestBot/UTBotCpp/tree/main/integration-tests/c-example/lib/types/types.c#L23"},"Source code example")),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"signed long long int max_long(long long a, signed long long b) {\n if (a > b) {\n return a;\n }\n return b;\n}\n")),(0,i.kt)("h6",{id:"tests-code"},"Tests code"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"TEST(regression, max_long_test_1)\n{\n // Construct input\n long long a = 0LL;\n long long b = -1LL;\n\n // Expected output\n long long expected = 0LL;\n\n // Trigger the function\n long long actual = max_long(a, b);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, max_long_test_2)\n{\n // Construct input\n long long a = 0LL;\n long long b = 0LL;\n\n // Expected output\n long long expected = 0LL;\n\n // Trigger the function\n long long actual = max_long(a, b);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n")),(0,i.kt)("h2",{id:"character-types"},"Character types"),(0,i.kt)("blockquote",null,(0,i.kt)("p",{parentName:"blockquote"},(0,i.kt)("inlineCode",{parentName:"p"},"char, signed char"))),(0,i.kt)("p",null,(0,i.kt)("a",{parentName:"p",href:"https://github.com/UnitTestBot/UTBotCpp/tree/main/integration-tests/c-example/lib/types/types.c#L42"},"Source code example")),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"signed char some_func(char a, unsigned char b) {\n if (b == 'z' && a > b) return a;\n if (b != 'z') return b;\n return '0';\n}\n")),(0,i.kt)("h6",{id:"tests-code-1"},"Tests code"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"TEST(regression, some_func_test_1)\n{\n // Construct input\n char a = 'a';\n unsigned char b = 122;\n\n // Expected output\n signed char expected = '0';\n\n // Trigger the function\n signed char actual = some_func(a, b);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, some_func_test_2)\n{\n // Construct input\n char a = 'a';\n unsigned char b = 120;\n\n // Expected output\n signed char expected = 'x';\n\n // Trigger the function\n signed char actual = some_func(a, b);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, some_func_test_3)\n{\n // Construct input\n char a = '{';\n unsigned char b = 122;\n\n // Expected output\n signed char expected = '{';\n\n // Trigger the function\n signed char actual = some_func(a, b);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(error, some_func_test_4)\n{\n // Construct input\n char a = 'a';\n unsigned char b = 128;\n\n // Trigger the function\n some_func(a, b);\n\n FAIL() << \"Unreachable point. Function was supposed to fail, but actually completed successfully.\";\n}\n")),(0,i.kt)("h2",{id:"floating-point-types"},"Floating-point types"),(0,i.kt)("blockquote",null,(0,i.kt)("p",{parentName:"blockquote"},(0,i.kt)("inlineCode",{parentName:"p"},"double, float, long double"))),(0,i.kt)("p",null,(0,i.kt)("a",{parentName:"p",href:"https://github.com/UnitTestBot/UTBotCpp/tree/main/integration-tests/c-example/lib/floats/floating_point.c#L30"},"Source code example")),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"float long_double_arith(long double x) {\n x *= 2;\n x -= 3.21;\n x *= fabsl(x);\n if (x == 1.0) {\n return 1.0;\n } else {\n return 3.5;\n }\n}\n")),(0,i.kt)("h6",{id:"tests-code-2"},"Tests code"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"TEST(regression, long_double_arith_test_1)\n{\n // Construct input\n long double x = 2.105000e+00;\n\n // Expected output\n float expected = 1.000000e+00;\n\n // Trigger the function\n float actual = long_double_arith(x);\n\n // Check results\n EXPECT_NEAR(expected, actual, utbot_abs_error);\n}\n\nTEST(regression, long_double_arith_test_2)\n{\n // Construct input\n long double x = 0.000000e+00;\n\n // Expected output\n float expected = 3.500000e+00;\n\n // Trigger the function\n float actual = long_double_arith(x);\n\n // Check results\n EXPECT_NEAR(expected, actual, utbot_abs_error);\n}\n")),(0,i.kt)("h2",{id:"_bool-type"},"_Bool type"),(0,i.kt)("blockquote",null,(0,i.kt)("p",{parentName:"blockquote"},"There is also a type alias bool for _Bool, defined in ",(0,i.kt)("inlineCode",{parentName:"p"},"<stdbool.h>"),".")),(0,i.kt)("p",null,(0,i.kt)("a",{parentName:"p",href:"https://github.com/UnitTestBot/UTBotCpp/tree/main/integration-tests/c-example/lib/types/types.c#L48"},"Source code example")),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"int fun_that_accept_bools(_Bool a, bool b) {\n if (a && b) return 1;\n if (a) return 2;\n if (b) return 3;\n return 4;\n}\n")),(0,i.kt)("h6",{id:"tests-code-3"},"Tests code"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"TEST(regression, fun_that_accept_bools_test_1)\n{\n // Construct input\n bool a = true;\n bool b = false;\n\n // Expected output\n int expected = 2;\n\n // Trigger the function\n int actual = fun_that_accept_bools(a, b);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, fun_that_accept_bools_test_2)\n{\n // Construct input\n bool a = false;\n bool b = false;\n\n // Expected output\n int expected = 4;\n\n // Trigger the function\n int actual = fun_that_accept_bools(a, b);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, fun_that_accept_bools_test_3)\n{\n // Construct input\n bool a = false;\n bool b = true;\n\n // Expected output\n int expected = 3;\n\n // Trigger the function\n int actual = fun_that_accept_bools(a, b);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, fun_that_accept_bools_test_4)\n{\n // Construct input\n bool a = true;\n bool b = true;\n\n // Expected output\n int expected = 1;\n\n // Trigger the function\n int actual = fun_that_accept_bools(a, b);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n")),(0,i.kt)("h2",{id:"structs-as-parameters"},"Structs as parameters"),(0,i.kt)("p",null,(0,i.kt)("a",{parentName:"p",href:"https://github.com/UnitTestBot/UTBotCpp/tree/main/integration-tests/c-example/lib/types/types.c#L122"},"Source code example")),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"struct SupportedStruct5 {\n short b;\n const int a;\n char c;\n};\n\nint structWithConstFields(struct SupportedStruct5 st) {\n if (st.a == 0) {\n return 1;\n }\n return 2;\n}\n")),(0,i.kt)("h6",{id:"tests-code-4"},"Tests code"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"TEST(regression, structWithConstFields_test_1)\n{\n // Construct input\n struct SupportedStruct5 st = {0, 0, 'c'};\n\n // Expected output\n int expected = 1;\n\n // Trigger the function\n int actual = structWithConstFields(st);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, structWithConstFields_test_2)\n{\n // Construct input\n struct SupportedStruct5 st = {0, -1, 'c'};\n\n // Expected output\n int expected = 2;\n\n // Trigger the function\n int actual = structWithConstFields(st);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n")),(0,i.kt)("h2",{id:"structs-as-return-values"},"Structs as return values"),(0,i.kt)("p",null,(0,i.kt)("a",{parentName:"p",href:"https://github.com/UnitTestBot/UTBotCpp/tree/main/integration-tests/c-example/lib/types/types.c#L84"},"Source code example")),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},'struct SupportedStruct4 {\n char* c;\n};\n\nconst struct SupportedStruct4 structWithConstPointerReturn(int a) {\n if (a % 2 == 0) {\n struct SupportedStruct4 res = {.c = "abcd"};\n return res;\n }\n struct SupportedStruct4 res = {.c = "ABCD"};\n return res;\n}\n')),(0,i.kt)("h6",{id:"tests-code-5"},"Tests code"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"TEST(regression, structWithConstPointerReturn_test_1)\n{\n // Construct input\n int a = 1;\n\n // Expected output\n struct SupportedStruct4 expected = {NULL};\n\n // Trigger the function\n const struct SupportedStruct4 actual = structWithConstPointerReturn(a);\n\n // Check results\n}\n\nTEST(regression, structWithConstPointerReturn_test_2)\n{\n // Construct input\n int a = 0;\n\n // Expected output\n struct SupportedStruct4 expected = {NULL};\n\n // Trigger the function\n const struct SupportedStruct4 actual = structWithConstPointerReturn(a);\n\n // Check results\n}\n")),(0,i.kt)("h2",{id:"unions"},"Unions"),(0,i.kt)("p",null,(0,i.kt)("a",{parentName:"p",href:"https://github.com/UnitTestBot/UTBotCpp/tree/main/integration-tests/c-example/lib/structures/simple_unions.c#L56"},"Source code example")),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"union MainUnion {\n union InnerUnion {\n union InInnerUnion {\n unsigned int u;\n long long l;\n };\n char c;\n union InInnerUnion ininner;\n short s;\n } inner;\n\n int x;\n long long y;\n};\n\nsigned char operate_with_inner_unions(union MainUnion st) {\n if (st.x == 5 || st.y == 5 || st.inner.c == '5' ||\n st.inner.s == 5 || st.inner.ininner.l == 5 || st.inner.ininner.u == 5) {\n return '5';\n }\n\n\n if (st.x == 5 || st.y == 102 || st.inner.s == 15) {\n return st.inner.c;\n }\n\n if ((long long) st.inner.ininner.u == st.inner.ininner.l) {\n return 'e';\n }\n if ((long long) st.inner.ininner.u > st.inner.ininner.l) {\n return 'g';\n }\n\n return 'o';\n}\n")),(0,i.kt)("h6",{id:"tests-code-6"},"Tests code"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"template<typename T, size_t N>\nT from_bytes(const char (&bytes)[N]) {\n T result;\n std::memcpy(&result, bytes, sizeof(result));\n return result;\n}\n\n\nTEST(regression, operate_with_inner_unions_test_1)\n{\n // Construct input\n union MainUnion st = from_bytes<MainUnion>({102, 0, 0, 0, 0, 0, 0, 0});\n\n\n // Expected output\n signed char expected = 'f';\n\n // Trigger the function\n signed char actual = operate_with_inner_unions(st);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, operate_with_inner_unions_test_2)\n{\n // Construct input\n union MainUnion st = from_bytes<MainUnion>({53, 0, 0, 0, 0, 0, 0, 0});\n\n // Expected output\n signed char expected = '5';\n\n // Trigger the function\n signed char actual = operate_with_inner_unions(st);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, operate_with_inner_unions_test_3)\n{\n // Construct input\n union MainUnion st = from_bytes<MainUnion>({15, 0, 0, 0, 0, 0, 0, 0});\n\n // Expected output\n signed char expected = '\\x0f';\n\n // Trigger the function\n signed char actual = operate_with_inner_unions(st);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, operate_with_inner_unions_test_4)\n{\n // Construct input\n union MainUnion st = from_bytes<MainUnion>({98, 0, 0, 0, 2, 0, 0, 0});\n\n // Expected output\n signed char expected = 'o';\n\n // Trigger the function\n signed char actual = operate_with_inner_unions(st);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, operate_with_inner_unions_test_5)\n{\n // Construct input\n union MainUnion st = from_bytes<MainUnion>({5, 0, -1, 0, 0, 0, 0, 0});\n\n // Expected output\n signed char expected = '5';\n\n // Trigger the function\n signed char actual = operate_with_inner_unions(st);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, operate_with_inner_unions_test_6)\n{\n // Construct input\n union MainUnion st = from_bytes<MainUnion>({104, 0, 0, 0, 0, 0, 0, 0});\n\n // Expected output\n signed char expected = 'e';\n\n // Trigger the function\n signed char actual = operate_with_inner_unions(st);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, operate_with_inner_unions_test_7)\n{\n // Construct input\n union MainUnion st = from_bytes<MainUnion>({5, 0, 0, 0, 0, 0, 0, 0});\n\n // Expected output\n signed char expected = '5';\n\n // Trigger the function\n signed char actual = operate_with_inner_unions(st);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, operate_with_inner_unions_test_8)\n{\n // Construct input\n union MainUnion st = from_bytes<MainUnion>({99, 0, 0, 0, 0, 0, 0, -128});\n\n // Expected output\n signed char expected = 'g';\n\n // Trigger the function\n signed char actual = operate_with_inner_unions(st);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n")),(0,i.kt)("h2",{id:"pointers-as-parameters-and-return-types"},"Pointers as parameters and return types"),(0,i.kt)("blockquote",null,(0,i.kt)("p",{parentName:"blockquote"},"If a pointer is used as a return value, UTBot is not yet capable of determining if it is used as an array, so only value under the pointer itself will be checked in generated tests.")),(0,i.kt)("p",null,(0,i.kt)("a",{parentName:"p",href:"https://github.com/UnitTestBot/UTBotCpp/tree/main/integration-tests/c-example/lib//pointers/pointer_parameters.c#L7"},"Source code example")),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"int c_strcmp(const char* a, const char *b) {\n for (int i = 0; ; i++) {\n if (a[i] != b[i]) {\n return 0;\n } else {\n if (a[i] == '\\0' || b[i] == '\\0') {\n return a[i] == '\\0' && b[i] == '\\0';\n }\n }\n }\n}\n\nint void_pointer_char_usage(void *x) {\n char *a = x;\n return c_strcmp(a, \"hello\");\n}\n\nint* five_square_numbers(int from) {\n static int sq[5];\n\n for (int i = 0; i < 5; i++) {\n sq[i] = from * from;\n from++;\n }\n\n return sq;\n}\n")),(0,i.kt)("h6",{id:"tests-code-7"},"Tests code"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},'TEST(regression, c_strcmp_test_1)\n{\n // Construct input\n char a_buffer[] = "";\n const char * a = a_buffer;\n char b_buffer[] = "";\n const char * b = b_buffer;\n\n // Expected output\n int expected = 1;\n\n // Trigger the function\n int actual = c_strcmp(a, b);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, c_strcmp_test_2)\n{\n // Construct input\n char a_buffer[] = "cbccccbccc";\n const char * a = a_buffer;\n char b_buffer[] = "cjcccccccc";\n const char * b = b_buffer;\n\n // Expected output\n int expected = 0;\n\n // Trigger the function\n int actual = c_strcmp(a, b);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, c_strcmp_test_3)\n{\n // Construct input\n char a_buffer[] = "abccccbcca";\n const char * a = a_buffer;\n char b_buffer[] = "icccccccci";\n const char * b = b_buffer;\n\n // Expected output\n int expected = 0;\n\n // Trigger the function\n int actual = c_strcmp(a, b);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(error, void_pointer_char_usage_test_1)\n{\n // Construct input\n __attribute__ ((aligned(128))) unsigned char x = 104;\n\n // Trigger the function\n void_pointer_char_usage(&x);\n\n FAIL() << "Unreachable point. Function was supposed to fail, but actually completed successfully.";\n}\n\nTEST(regression, void_pointer_char_usage_test_2)\n{\n // Construct input\n __attribute__ ((aligned(128))) unsigned char x = 0;\n\n // Expected output\n int expected = 0;\n\n // Trigger the function\n int actual = void_pointer_char_usage(&x);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, five_square_numbers_test_1)\n{\n // Construct input\n int from = 0;\n\n // Expected output\n int expected[1] = {0};\n\n // Trigger the function\n int * actual = five_square_numbers(from);\n\n // Check results\n for (int it_0_0 = 0; it_0_0 < 1; it_0_0 ++) {\n EXPECT_EQ(expected[it_0_0], actual[it_0_0]);\n }\n}\n')),(0,i.kt)("h2",{id:"pointers-as-struct-fields-members"},"Pointers as struct fields members"),(0,i.kt)("p",null,(0,i.kt)("a",{parentName:"p",href:"https://github.com/UnitTestBot/UTBotCpp/tree/main/integration-tests/c-example/lib//structures/structs/structs_with_pointers.c#L28"},"Source code example")),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"struct List {\n struct List * next;\n int val;\n};\n\nint list_sum_sign(struct List *head) {\n int sum = list_sum(head);\n if (sum > 0) {\n return 1;\n } else if (sum < 0) {\n return -1;\n } else {\n return 0;\n }\n}\n")),(0,i.kt)("h6",{id:"tests-code-8"},"Tests code"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"TEST(regression, list_sum_sign_test_1)\n{\n // Construct input\n struct List head = {NULL, 0};\n\n // Expected output\n int expected = 0;\n\n // Trigger the function\n int actual = list_sum_sign(&head);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, list_sum_sign_test_2)\n{\n // Construct input\n struct List head = {NULL, -10};\n\n // Expected output\n int expected = -1;\n\n // Trigger the function\n int actual = list_sum_sign(&head);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, list_sum_sign_test_3)\n{\n // Construct input\n struct List head = {NULL, 1};\n\n // Expected output\n int expected = 1;\n\n // Trigger the function\n int actual = list_sum_sign(&head);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n")),(0,i.kt)("h2",{id:"pointers-to-functions"},"Pointers to functions"),(0,i.kt)("blockquote",null,(0,i.kt)("ul",{parentName:"blockquote"},(0,i.kt)("li",{parentName:"ul"},"If return type is a pointer to function, UTBot doesn't checking expected value - comparing pointers doesn't make any sense."),(0,i.kt)("li",{parentName:"ul"},"We support arrays of pointers to functions also, but 1-dimensional only."),(0,i.kt)("li",{parentName:"ul"},"If a function takes pointer to another function as parameter, UTBot generates stub for this parameter."))),(0,i.kt)("p",null,(0,i.kt)("a",{parentName:"p",href:"https://github.com/UnitTestBot/UTBotCpp/tree/main/integration-tests/c-example/lib/pointers/function_pointers.c#L66"},"Source code example")),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"typedef int (*op_func)(int, int);\n\nint f_add(int a, int b) {\n return a + b;\n}\nint f_sub(int a, int b) {\n return a - b;\n}\nint f_mul(int a, int b) {\n return a * b;\n}\n\nop_func return_op(char op) {\n switch(op) {\n case '+': return f_add;\n case '-': return f_sub;\n case '*': return f_mul;\n }\n return NULL;\n}\n\nchar* pointerParam(char* (*f)(int*), int* x) {\n if (*x == 1) {\n return f(x);\n } else{\n return f(x + 5);\n }\n}\n")),(0,i.kt)("h6",{id:"tests-code-9"},"Tests code"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"\nTEST(regression, return_op_test_1)\n{\n// Construct input\nchar op = '*';\n\n// Expected output\n// No output variable check for function returning pointer to function\n\n// Trigger the function\nreturn_op(op);\n\n// Check results\n// No check results for function returning pointer to function\n}\n\nTEST(regression, return_op_test_2)\n{\n// Construct input\nchar op = 'c';\n\n// Expected output\n// No output variable check for function returning pointer to function\n\n// Trigger the function\nreturn_op(op);\n\n// Check results\n// No check results for function returning pointer to function\n}\n\nTEST(regression, return_op_test_3)\n{\n// Construct input\nchar op = '-';\n\n// Expected output\n// No output variable check for function returning pointer to function\n\n// Trigger the function\nreturn_op(op);\n\n// Check results\n// No check results for function returning pointer to function\n}\n\nTEST(regression, return_op_test_4)\n{\n// Construct input\nchar op = '+';\n\n// Expected output\n// No output variable check for function returning pointer to function\n\n// Trigger the function\nreturn_op(op);\n\n// Check results\n// No check results for function returning pointer to function\n}\n\n\ntypedef char * (*pointerParam_f_arg)(int *);\nchar * _pointerParam_f_stub(int * param1) {\nreturn \"\";\n}\n\nTEST(regression, pointerParam_test_1)\n{\n// Construct input\npointerParam_f_arg f = *_pointerParam_f_stub;\nint x = 0;\n\n// Expected output\nchar expected = '\\0';\n\n// Trigger the function\nchar actual = *pointerParam(f, &x);\n\n// Check results\nEXPECT_EQ(expected, actual);\n}\n\nTEST(regression, pointerParam_test_2)\n{\n// Construct input\npointerParam_f_arg f = *_pointerParam_f_stub;\nint x = 1;\n\n// Expected output\nchar expected = '\\0';\n\n// Trigger the function\nchar actual = *pointerParam(f, &x);\n\n// Check results\nEXPECT_EQ(expected, actual);\n}\n\n")),(0,i.kt)("h2",{id:"arrays"},"Arrays"),(0,i.kt)("p",null,(0,i.kt)("a",{parentName:"p",href:"https://github.com/UnitTestBot/UTBotCpp/tree/main/integration-tests/c-example/lib/structures/struct_arrays.c#L18"},"Source code example")),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"struct Trio {\n int a;\n long long b;\n short c;\n};\n\nint index_of_struct_with_equal_fields(struct Trio arr []) {\n for (int i = 0; i < 10; i++) {\n if (arr[i].a == arr[i].b) {\n if (arr[i].b == arr[i].c) {\n return i; \n }\n }\n }\n\n return -1;\n}\n")),(0,i.kt)("h6",{id:"tests-code-10"},"Tests code"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"TEST(regression, index_of_struct_with_equal_fields_test_1)\n{\n // Construct input\n struct Trio arr[10] = {{0, 1LL, 0}, {0, 4LL, 0}, {0, 1LL, 0}, {0, 2LL, 0}, {0, 8LL, 0}, {0, 8LL, 0}, {0, 4LL, 0}, {0, 4LL, 0}, {0, 1LL, 0}, {0, 1LL, 0}};\n\n // Expected output\n int expected = -1;\n\n // Trigger the function\n int actual = index_of_struct_with_equal_fields(arr);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, index_of_struct_with_equal_fields_test_2)\n{\n // Construct input\n struct Trio arr[10] = {{0, 1LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}};\n\n // Expected output\n int expected = 1;\n\n // Trigger the function\n int actual = index_of_struct_with_equal_fields(arr);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, index_of_struct_with_equal_fields_test_3)\n{\n // Construct input\n struct Trio arr[10] = {{0, 1LL, 0}, {8, 8LL, 0}, {2, 2LL, 0}, {0, 2LL, 0}, {1, 1LL, 0}, {1, 1LL, 0}, {0, 4LL, 0}, {1, 1LL, 0}, {0, 1LL, 0}, {2, 2LL, 0}};\n\n // Expected output\n int expected = -1;\n\n // Trigger the function\n int actual = index_of_struct_with_equal_fields(arr);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, index_of_struct_with_equal_fields_test_4)\n{\n // Construct input\n struct Trio arr[10] = {{2, 2LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}};\n\n // Expected output\n int expected = 1;\n\n // Trigger the function\n int actual = index_of_struct_with_equal_fields(arr);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, index_of_struct_with_equal_fields_test_5)\n{\n // Construct input\n struct Trio arr[10] = {{0, 1LL, 0}, {0, 4LL, 0}, {0, 1LL, 0}, {2, 2LL, 0}, {0, 8LL, 0}, {0, 8LL, 0}, {0, 4LL, 0}, {0, 4LL, 0}, {2, 2LL, 0}, {2, 2LL, 0}};\n\n // Expected output\n int expected = -1;\n\n // Trigger the function\n int actual = index_of_struct_with_equal_fields(arr);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, index_of_struct_with_equal_fields_test_6)\n{\n // Construct input\n struct Trio arr[10] = {{0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}, {0, 0LL, 0}};\n\n // Expected output\n int expected = 0;\n\n // Trigger the function\n int actual = index_of_struct_with_equal_fields(arr);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n")),(0,i.kt)("h2",{id:"multidimensional-arrays-and-pointers"},"Multidimensional arrays and pointers"),(0,i.kt)("blockquote",null,(0,i.kt)("p",{parentName:"blockquote"},"Arrays of any dimensions are supported, but as for pointers, only 1-d and 2-d are supported.")),(0,i.kt)("p",null,(0,i.kt)("a",{parentName:"p",href:"https://github.com/UnitTestBot/UTBotCpp/tree/main/integration-tests/c-example/lib/multi_arrays.c#L67"},"Source code example")),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"int some_method(int ** pointer2d) {\n int x = 2;\n for (int i = 0; i < 2; i++) {\n for (int j = 0; j < 2; j++) {\n if (pointer2d[i][j] > 0) {\n return i * 2 + j;\n }\n }\n }\n return -1;\n}\n")),(0,i.kt)("h6",{id:"tests-code-11"},"Tests code"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"TEST(regression, some_method_test_1)\n{\n // Construct input\n int _pointer2d[2][2] = {{0, 0}, {0, 0}};\n int ** pointer2d = (int **) calloc(3, sizeof(int *));\n for (int it_9_0 = 0; it_9_0 < 2; it_9_0 ++) {\n pointer2d[it_9_0] = _pointer2d[it_9_0];\n }\n pointer2d[2] = NULL;\n\n // Expected output\n int expected = -1;\n\n // Trigger the function\n int actual = some_method(pointer2d);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, some_method_test_2)\n{\n // Construct input\n int _pointer2d[2][2] = {{0, 1}, {0, 0}};\n int ** pointer2d = (int **) calloc(3, sizeof(int *));\n for (int it_9_0 = 0; it_9_0 < 2; it_9_0 ++) {\n pointer2d[it_9_0] = _pointer2d[it_9_0];\n }\n pointer2d[2] = NULL;\n\n // Expected output\n int expected = 1;\n\n // Trigger the function\n int actual = some_method(pointer2d);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, some_method_test_3)\n{\n // Construct input\n int _pointer2d[2][2] = {{0, 0}, {1, 0}};\n int ** pointer2d = (int **) calloc(3, sizeof(int *));\n for (int it_9_0 = 0; it_9_0 < 2; it_9_0 ++) {\n pointer2d[it_9_0] = _pointer2d[it_9_0];\n }\n pointer2d[2] = NULL;\n\n // Expected output\n int expected = 2;\n\n // Trigger the function\n int actual = some_method(pointer2d);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, some_method_test_4)\n{\n // Construct input\n int _pointer2d[2][2] = {{1, 0}, {0, 0}};\n int ** pointer2d = (int **) calloc(3, sizeof(int *));\n for (int it_9_0 = 0; it_9_0 < 2; it_9_0 ++) {\n pointer2d[it_9_0] = _pointer2d[it_9_0];\n }\n pointer2d[2] = NULL;\n\n // Expected output\n int expected = 0;\n\n // Trigger the function\n int actual = some_method(pointer2d);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n")),(0,i.kt)("h2",{id:"enums"},"Enums"),(0,i.kt)("p",null,(0,i.kt)("a",{parentName:"p",href:"https://github.com/UnitTestBot/UTBotCpp/tree/main/integration-tests/c-example/lib/structures/enums.c#L9"},"Source code example")),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"enum Sign {\n NEGATIVE,\n ZERO,\n POSITIVE\n};\n\n\nint enumSignToInt(enum Sign s) {\n if (s == ZERO) {\n return 0;\n }\n if (s == NEGATIVE) {\n return -1;\n } else {\n return 1;\n } \n}\n\nint enumSignPointerToInt(enum Sign *s) {\n return enumSignToInt(*s);\n}\n")),(0,i.kt)("h6",{id:"tests-code-12"},"Tests code"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"TEST(regression, enumSignToInt_test_1)\n{\n // Construct input\n enum Sign s = POSITIVE;\n\n // Expected output\n int expected = 1;\n\n // Trigger the function\n int actual = enumSignToInt(s);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, enumSignToInt_test_2)\n{\n // Construct input\n enum Sign s = NEGATIVE;\n\n // Expected output\n int expected = -1;\n\n // Trigger the function\n int actual = enumSignToInt(s);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, enumSignToInt_test_3)\n{\n // Construct input\n enum Sign s = ZERO;\n\n // Expected output\n int expected = 0;\n\n // Trigger the function\n int actual = enumSignToInt(s);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\n\nTEST(regression, enumSignPointerToInt_test_1)\n{\n // Construct input\n enum Sign s = NEGATIVE;\n\n // Expected output\n int expected = -1;\n\n // Trigger the function\n int actual = enumSignPointerToInt(&s);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, enumSignPointerToInt_test_2)\n{\n // Construct input\n enum Sign s = POSITIVE;\n\n // Expected output\n int expected = 1;\n\n // Trigger the function\n int actual = enumSignPointerToInt(&s);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, enumSignPointerToInt_test_3)\n{\n // Construct input\n enum Sign s = ZERO;\n\n // Expected output\n int expected = 0;\n\n // Trigger the function\n int actual = enumSignPointerToInt(&s);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n")),(0,i.kt)("h2",{id:"typedef"},"Typedef"),(0,i.kt)("p",null,(0,i.kt)("a",{parentName:"p",href:"https://github.com/UnitTestBot/UTBotCpp/tree/main/integration-tests/c-example/lib/types/typedefs_1.c#L20"},"Source code example")),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"typedef struct __typeDefStruct {\n int a;\n} TypeDefStruct2;\n\nint sign_of_typedef_struct2(TypeDefStruct2 x) {\n if (x.a > 0) {\n return 1;\n }\n\n if (x.a < 0) {\n return -1;\n }\n\n return 0;\n}\n")),(0,i.kt)("h6",{id:"tests-code-13"},"Tests code"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"TEST(regression, sign_of_typedef_struct2_test_1)\n{\n // Construct input\n TypeDefStruct2 x = {0};\n\n // Expected output\n int expected = 0;\n\n // Trigger the function\n int actual = sign_of_typedef_struct2(x);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, sign_of_typedef_struct2_test_2)\n{\n // Construct input\n TypeDefStruct2 x = {-10};\n\n // Expected output\n int expected = -1;\n\n // Trigger the function\n int actual = sign_of_typedef_struct2(x);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, sign_of_typedef_struct2_test_3)\n{\n // Construct input\n TypeDefStruct2 x = {1};\n\n // Expected output\n int expected = 1;\n\n // Trigger the function\n int actual = sign_of_typedef_struct2(x);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n")),(0,i.kt)("h2",{id:"static-functions"},"Static functions"),(0,i.kt)("p",null,(0,i.kt)("a",{parentName:"p",href:"https://github.com/UnitTestBot/UTBotCpp/tree/main/integration-tests/c-example/lib/static.c#L8"},"Source code example")),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"static int static_simple(int dx)\n{\n if (x > 0)\n {\n return x + dx;\n }\n if (x < 0)\n {\n return -x + dx;\n }\n return 0;\n}\n")),(0,i.kt)("h6",{id:"tests-code-14"},"Tests code"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"TEST(regression, static_simple_test_1)\n{\n // Initialize global variables\n x = 0;\n\n // Construct input\n int dx = 0;\n\n // Expected output\n int expected = 0;\n\n // Trigger the function\n int actual = static_simple(dx);\n\n // Check results\n EXPECT_EQ(expected, actual);\n // Check global variables\n int expected_x = 0;\n EXPECT_EQ(expected_x, x);\n}\n\nTEST(regression, static_simple_test_2)\n{\n // Initialize global variables\n x = -10;\n\n // Construct input\n int dx = -10;\n\n // Expected output\n int expected = 0;\n\n // Trigger the function\n int actual = static_simple(dx);\n\n // Check results\n EXPECT_EQ(expected, actual);\n // Check global variables\n int expected_x = -10;\n EXPECT_EQ(expected_x, x);\n}\n\nTEST(regression, static_simple_test_3)\n{\n // Initialize global variables\n x = 1;\n\n // Construct input\n int dx = -1;\n\n // Expected output\n int expected = 0;\n\n // Trigger the function\n int actual = static_simple(dx);\n\n // Check results\n EXPECT_EQ(expected, actual);\n // Check global variables\n int expected_x = 1;\n EXPECT_EQ(expected_x, x);\n}\n")),(0,i.kt)("h2",{id:"qualifiers-const-volatile-restrict-etc"},"Qualifiers: const, volatile, restrict etc."),(0,i.kt)("p",null,(0,i.kt)("a",{parentName:"p",href:"https://github.com/UnitTestBot/UTBotCpp/tree/main/integration-tests/c-example/lib/keywords/qualifiers.c#L49"},"Source code example")),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},'const char * const foo_bar(volatile int a) {\n if (a < 0) {\n return "-1";\n } else if (a == 0) {\n return "0";\n } else {\n return "1";\n }\n}\n')),(0,i.kt)("h6",{id:"tests-code-15"},"Tests code"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"TEST(regression, foo_bar_test_1)\n{\n // Construct input\n int a = 2;\n\n // Expected output\n char expected = '1';\n\n // Trigger the function\n const char actual = *foo_bar(a);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, foo_bar_test_2)\n{\n // Construct input\n int a = 0;\n\n // Expected output\n char expected = '0';\n\n // Trigger the function\n const char actual = *foo_bar(a);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n\nTEST(regression, foo_bar_test_3)\n{\n // Construct input\n int a = -1;\n\n // Expected output\n char expected = '-';\n\n // Trigger the function\n const char actual = *foo_bar(a);\n\n // Check results\n EXPECT_EQ(expected, actual);\n}\n")),(0,i.kt)("h2",{id:"global-variables"},"Global variables"),(0,i.kt)("p",null,(0,i.kt)("a",{parentName:"p",href:"https://github.com/UnitTestBot/UTBotCpp/tree/main/integration-tests/c-example/lib/globals.c#L64"},"Source code example")),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"static char* global_mutable_string;\nstatic const char* global_const_string;\n\n\nchar use_global_strings() {\n if (!global_mutable_string) {\n return 'M';\n }\n if (!global_const_string) {\n return 'C';\n }\n char c = global_const_string[0];\n char res;\n if (c >= 'a' && c <= 'z') {\n res = 'A' + c - 'a';\n } else {\n res = c;\n }\n global_mutable_string[0] = res;\n return res;\n}\n")),(0,i.kt)("h6",{id:"tests-code-16"},"Tests code"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-cpp"},"TEST(regression, use_global_strings_test_1)\n{\n // Initialize global variables\n char global_mutable_string_buffer[] = \"ccacccbbbc\";\n global_mutable_string = global_mutable_string_buffer;\n char global_const_string_buffer[] = \"{ccaccccc{\";\n global_const_string = global_const_string_buffer;\n\n\n // Expected output\n char expected = '{';\n\n // Trigger the function\n char actual = use_global_strings();\n\n // Check results\n EXPECT_EQ(expected, actual);\n // Check global variables\n char expected_global_mutable_string = '{';\n EXPECT_EQ(expected_global_mutable_string, (*global_mutable_string));\n char expected_global_const_string = '{';\n EXPECT_EQ(expected_global_const_string, (*global_const_string));\n}\n\nTEST(regression, use_global_strings_test_2)\n{\n // Initialize global variables\n char global_mutable_string_buffer[] = \"ccacccbbbc\";\n global_mutable_string = global_mutable_string_buffer;\n char global_const_string_buffer[] = \"cccacccccc\";\n global_const_string = global_const_string_buffer;\n\n\n // Expected output\n char expected = 'C';\n\n // Trigger the function\n char actual = use_global_strings();\n\n // Check results\n EXPECT_EQ(expected, actual);\n // Check global variables\n char expected_global_mutable_string = 'C';\n EXPECT_EQ(expected_global_mutable_string, (*global_mutable_string));\n char expected_global_const_string = 'c';\n EXPECT_EQ(expected_global_const_string, (*global_const_string));\n}\n\nTEST(regression, use_global_strings_test_3)\n{\n // Initialize global variables\n char global_mutable_string_buffer[] = \"ccacccbbbc\";\n global_mutable_string = global_mutable_string_buffer;\n char global_const_string_buffer[] = \"\";\n global_const_string = global_const_string_buffer;\n\n\n // Expected output\n char expected = '\\0';\n\n // Trigger the function\n char actual = use_global_strings();\n\n // Check results\n EXPECT_EQ(expected, actual);\n // Check global variables\n char expected_global_mutable_string = '\\0';\n EXPECT_EQ(expected_global_mutable_string, (*global_mutable_string));\n char expected_global_const_string = '\\0';\n EXPECT_EQ(expected_global_const_string, (*global_const_string));\n}\n")))}l&&l===Object(l)&&Object.isExtensible(l)&&!Object.prototype.hasOwnProperty.call(l,"__filemeta")&&Object.defineProperty(l,"__filemeta",{configurable:!0,value:{name:"MDXContent",filename:"src/docs/advanced/c-syntax.md"}}),l.isMDXComponent=!0}}]);
//# sourceMappingURL=component---src-docs-advanced-c-syntax-md-1b5ca7527bbcd51a9451.js.map