Skip to content

Commit 2031819

Browse files
committed
Merge branch 'devel'
Conflicts: project_manager.sh
2 parents 40cea6b + 489e2a6 commit 2031819

File tree

12 files changed

+281
-14
lines changed

12 files changed

+281
-14
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
*.hex
1111
*.d
1212
*.a
13+
*.h.cpp
14+
*.ocxx
15+
*.cxxtest
1316

1417
# Packages #
1518
############
@@ -29,6 +32,7 @@
2932
*.log
3033
*.sql
3134
*.sqlite
35+
*.gdb_history
3236

3337
# OS generated files #
3438
######################

.gitmodules

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
[submodule "submodules/googletest"]
88
path = submodules/googletest
99
url = https://github.com/google/googletest
10+
ignore = dirty

ReleaseNote.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,16 @@ RelNote -*- mode : org -*-
2828
- State "DONE" from "INPROGRESS" [2017-06-12 Mon 20:05]
2929
*** DONE add simple gtest project
3030
- State "DONE" from "INPROGRESS" [2017-06-12 Mon 20:05]
31-
** INPROGRESS full range adc & pin change interrupt C support
31+
** WAIT full range adc & pin change interrupt C support
32+
** DONE add checking dependency in fresh repo clone
33+
- State "DONE" from "FEEDBACK" [2017-07-29 Sat 20:14]
34+
- State "FEEDBACK" from "INPROGRESS" [2017-07-29 Sat 20:14]
35+
** DONE fix segfault & stack smashing in serial calc project
36+
- State "DONE" from "CANCELED" [2017-07-30 Sun 13:45]
37+
** DONE move serial calc project unittest to gtes
38+
- State "DONE" from "CANCELED" [2017-07-30 Sun 13:45]
39+
** DONE avoid recompiling gtest library every time running make check
40+
- State "DONE" from "CANCELED" [2017-07-30 Sun 13:46]
3241
** TODO motion sensor test code
3342
** TODO hand clap sound detector
3443
* WAIT v2.2.0 - more integrated module in i2c-console

c-project/without-os/serial-calculator/InfixConverter.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
#include "InfixConverter.h"
99
#include "AVRString.h"
1010

11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
14+
1115
#include <stdio.h>
1216
#include <string.h>
1317
#include <ctype.h>
1418

15-
const char g_infix_supported_ops[] = "+-*/()";
19+
static const char g_infix_supported_ops[] = "+-*/()";
1620

1721
/**
1822
* Compare precedence between a and b
@@ -100,10 +104,11 @@ uint8_t addSpaces(char * buffer)
100104
}
101105
}
102106
buffer2[index2] = '\0';
103-
AVRStrinStripExtraSpace(buffer2);
104-
AVRStringTrimWhiteSpace(buffer2);
107+
char * buffer3 = buffer2;
108+
AVRStrinStripExtraSpace(buffer3);
109+
AVRStringTrimWhiteSpace(buffer3);
105110

106-
strcpy(buffer, buffer2);
111+
strcpy(buffer, buffer3);
107112

108113
return 0;
109114
}
@@ -298,8 +303,13 @@ uint8_t InfixConverterConvertString(const char * input, char * output)
298303
}
299304
else
300305
{
301-
strcpy(output, AVRStringTrimWhiteSpace(output));
306+
strcpy(output_tmp, AVRStringTrimWhiteSpace(output));
307+
strcpy(output, output_tmp);
302308
}
303309

304310
return retVal;
305311
}
312+
313+
#ifdef __cplusplus
314+
}
315+
#endif

c-project/without-os/serial-calculator/InfixConverter.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#ifndef SERIAL_CALCULATOR_INFIXCONVERTER_H_
99
#define SERIAL_CALCULATOR_INFIXCONVERTER_H_
1010

11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
14+
1115
#include <inttypes.h>
1216

1317
/**
@@ -25,4 +29,8 @@
2529
*/
2630
uint8_t InfixConverterConvertString(const char * input, char * output);
2731

32+
#ifdef __cplusplus
33+
}
34+
#endif
35+
2836
#endif /* SERIAL_CALCULATOR_INFIXCONVERTER_H_ */

c-project/without-os/serial-calculator/Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ TOP = ../../../
1212

1313
# unit test files
1414
# remember to put cxx test headers in test/*Test.h
15-
UNITTEST_SUPPORT = yes
15+
# or gtest test .cpp files in test/*gtest.cpp
16+
UNITTEST_SUPPORT = no
17+
GTEST_SUPPORT = yes
1618
TEST_SOURCES = InfixConverter.c PostfixCalculator.c $(UTILS_DIR)/AVRString.c
1719
TEST_DEBUG = no
18-
TEST_CFLAGS = -std=gnu++0x
20+
TEST_CFLAGS = -fno-stack-protector
1921

2022
# always include skeleton.mk
2123
include $(TOP)/common/skeleton.mk

c-project/without-os/serial-calculator/test/InfixConverterTest.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
#include <cxxtest/TestSuite.h>
1212
#include <string>
1313
#include <iostream>
14-
1514
#include "InfixConverter.h"
1615

1716
using namespace std;
1817

19-
extern uint8_t addSpaces(char * buffer);
18+
extern "C" {
19+
uint8_t addSpaces(char * buffer);
20+
}
2021

2122
class myTS: public CxxTest::TestSuite
2223
{
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* InfixConverterTest.h
3+
*
4+
* Created on: Jun 2, 2017
5+
* Author: dat
6+
*/
7+
8+
#include <limits.h>
9+
#include "gtest/gtest.h"
10+
11+
#include <string>
12+
#include <iostream>
13+
#include "InfixConverter.h"
14+
15+
using namespace std;
16+
17+
extern "C" {
18+
uint8_t addSpaces(char * buffer);
19+
}
20+
21+
namespace
22+
{
23+
class MyTest : public ::testing::Test
24+
{
25+
protected:
26+
MyTest()
27+
{
28+
29+
}
30+
31+
virtual ~MyTest()
32+
{
33+
34+
}
35+
36+
virtual void SetUp()
37+
{
38+
output[0] = '\0';
39+
}
40+
41+
virtual void TearDown()
42+
{
43+
44+
}
45+
46+
char output[256];
47+
};
48+
}
49+
50+
TEST_F(MyTest, testConverter2)
51+
{
52+
EXPECT_EQ(InfixConverterConvertString("1+2+3", output), 0);
53+
EXPECT_STREQ(output, "1 2 + 3 +");
54+
55+
EXPECT_EQ(InfixConverterConvertString("1+(2*3)", output), 0);
56+
EXPECT_STREQ(output, "1 2 3 * +");
57+
}
58+
59+
TEST_F(MyTest, testConverter1)
60+
{
61+
EXPECT_EQ(InfixConverterConvertString("123", output), 0);
62+
EXPECT_STREQ(output, "123");
63+
64+
EXPECT_EQ(InfixConverterConvertString(" 65555 ", output), 0);
65+
EXPECT_STREQ(output, "65555");
66+
67+
EXPECT_EQ(InfixConverterConvertString("1 + 2", output), 0);
68+
EXPECT_STREQ(output, "1 2 +");
69+
}
70+
71+
TEST_F(MyTest, testConverter_Success_Prior)
72+
{
73+
EXPECT_EQ(InfixConverterConvertString("1*2+3", output), 0);
74+
EXPECT_STREQ(output, "1 2 * 3 +");
75+
76+
EXPECT_EQ(InfixConverterConvertString("1*2+3*4", output), 0);
77+
EXPECT_STREQ(output, "1 2 * 3 4 * +");
78+
79+
EXPECT_EQ(InfixConverterConvertString("1/2+3*4", output), 0);
80+
EXPECT_STREQ(output, "1 2 / 3 4 * +");
81+
82+
EXPECT_EQ(InfixConverterConvertString("1*2+3 *4 - 5 / 6", output), 0);
83+
EXPECT_STREQ(output, "1 2 * 3 4 * + 5 6 / -");
84+
}
85+
86+
TEST_F(MyTest, testConverter_FailFormat)
87+
{
88+
EXPECT_EQ(InfixConverterConvertString("(1", output), 1);
89+
EXPECT_EQ(InfixConverterConvertString(")1(", output), 1);
90+
}
91+
92+
TEST_F(MyTest, testConverter_Fail_UnsupportedChar)
93+
{
94+
EXPECT_EQ(InfixConverterConvertString("1.2", output), 2);
95+
EXPECT_EQ(InfixConverterConvertString("1^2", output), 2);
96+
}
97+
98+
TEST_F(MyTest, testConverter_ExtraSpace)
99+
{
100+
char input[] = " A+B";
101+
char input2[] = "+-*/()";
102+
103+
EXPECT_EQ(addSpaces(input), 0);
104+
EXPECT_STREQ(input, "A + B");
105+
106+
EXPECT_EQ(addSpaces(input2), 0);
107+
EXPECT_STREQ(input2, "+ - * / ( )");
108+
}
109+
110+
TEST_F(MyTest, testConverter_1parentpair1)
111+
{
112+
EXPECT_EQ(InfixConverterConvertString("1*(2+3)", output), 0);
113+
EXPECT_STREQ(output, "1 2 3 + *");
114+
115+
EXPECT_EQ(InfixConverterConvertString("(1+2)", output), 0);
116+
EXPECT_STREQ(output, "1 2 +");
117+
}
118+
119+
TEST_F(MyTest, testConverter_Comprehensive)
120+
{
121+
EXPECT_EQ(InfixConverterConvertString("1* (2+3 * 4) +5", output), 0);
122+
EXPECT_STREQ(output, "1 2 3 4 * + * 5 +");
123+
124+
EXPECT_EQ(InfixConverterConvertString("111* (222+333 * 444) +555", output), 0);
125+
EXPECT_STREQ(output, "111 222 333 444 * + * 555 +");
126+
127+
EXPECT_EQ(InfixConverterConvertString("1*(2-3/4) +5/6*(7+8)", output), 0);
128+
EXPECT_STREQ(output, "1 2 3 4 / - * 5 6 / 7 8 + * +");
129+
}
130+
131+
132+
133+
134+
135+
136+
137+
138+
139+
140+
141+
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* InfixCalculatorComputeTest.h
3+
*
4+
* Created on: Jun 3, 2017
5+
* Author: dat
6+
*/
7+
8+
#include "gtest/gtest.h"
9+
#include <limits.h>
10+
11+
#include <string>
12+
#include <iostream>
13+
#include <inttypes.h>
14+
15+
#include "PostfixCalculator.h"
16+
17+
using namespace std;
18+
19+
extern "C" {
20+
uint8_t addSpaces(char * buffer);
21+
}
22+
23+
namespace
24+
{
25+
class MyTest : public ::testing::Test
26+
{
27+
protected:
28+
MyTest() : output(INT_MAX)
29+
{
30+
31+
}
32+
33+
virtual ~MyTest()
34+
{
35+
36+
}
37+
38+
virtual void SetUp()
39+
{
40+
output = INT_MAX;
41+
}
42+
43+
virtual void TearDown()
44+
{
45+
46+
}
47+
48+
int32_t output;
49+
};
50+
}
51+
52+
TEST_F(MyTest, testConverter2)
53+
{
54+
}
55+
56+
TEST_F(MyTest, test_noop_success)
57+
{
58+
EXPECT_EQ(PostfixCalculatorCompute("1"), 1);
59+
EXPECT_EQ(PostfixCalculatorCompute("1123"), 1123);
60+
EXPECT_EQ(PostfixCalculatorCompute("0"), 0);
61+
}
62+
63+
TEST_F(MyTest, test_1op_success)
64+
{
65+
EXPECT_EQ(PostfixCalculatorCompute("1 2 +"), 3);
66+
EXPECT_EQ(PostfixCalculatorCompute("1 2 *"), 2);
67+
EXPECT_EQ(PostfixCalculatorCompute("1 2 -"), -1);
68+
EXPECT_EQ(PostfixCalculatorCompute("2 1 /"), 2);
69+
EXPECT_EQ(PostfixCalculatorCompute("1 2 /"), 0);
70+
}
71+
72+
TEST_F(MyTest, test_comprehensive)
73+
{
74+
EXPECT_EQ(PostfixCalculatorCompute("1 2 * 3 4 * + 5 6 / -"), 14);
75+
}
76+
77+
TEST_F(MyTest, test_fail)
78+
{
79+
EXPECT_EQ(PostfixCalculatorCompute("1 + 2"), INT32_MIN);
80+
EXPECT_EQ(PostfixCalculatorCompute("1 + +"), INT32_MIN);
81+
EXPECT_EQ(PostfixCalculatorCompute("1 2"), INT32_MIN);
82+
EXPECT_EQ(PostfixCalculatorCompute(""), INT32_MIN);
83+
}

common/makefiles/gtest.mk

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
55
MKFILE_DIRNAME := $(notdir $(patsubst %/,%,$(dir $(MKFILE_PATH))))
66
TOP :=$(shell dirname $(MKFILE_PATH))/../../
7+
TOP :=$(shell readlink -f $(TOP))
78

89
# gtest library definition
910
GTEST_DIR = $(TOP)/submodules/googletest/googletest
1011
GTEST_SRC = $(wildcard $(GTEST_DIR)/src/*.cc)
11-
GTEST_LIB = gtest.a
12+
GTEST_LIB = $(GTEST_DIR)/gtest.a
1213
TEST_CFLAGS += -isystem $(GTEST_DIR)/include
1314
INCLUDES += $(GTEST_DIR)/include/gtest/ $(GTEST_DIR)/include/gtest/internal/ $(GTEST_DIR)/
1415
LDFLAGS += -pthread -lpthread
@@ -74,5 +75,5 @@ $(GTEST_LIB): $(GTEST_OBJS)
7475

7576
cleantest:
7677
rm -f $(TEST_OBJECTS) $(TEST_BIN_OBJS) $(TEST_BINARIES)
77-
rm -f $(GTEST_LIB) $(GTEST_OBJS)
78+
# no need to remove GTEST_SRC and GTEST_LIB since we don't edit gtest src
7879

0 commit comments

Comments
 (0)