2016年10月8日土曜日

開発環境

テスト駆動開発による組み込みプログラミング (James W. Grenning (著)、蛸島 昭之 (監修)、笹井 崇司(翻訳)、オライリージャパン)のI部(TDDを始めよう)、3章(Cモジュールにとりかかる)、3.10(練習問題)、1、3.を取り組んでみる。

1、3.

コード(Emacs)

Makefile

#Set this to @ to keep the makefile quiet
SILENCE = @

#---- Outputs ----#
COMPONENT_NAME = BookCode_Unity
TARGET1 = test1

#--- Inputs ----#
UNITY_HOME = $(HOME)/root/Unity
CPP_PLATFORM = Gcc
PROJECT_HOME_DIR = .
PROJECT_TEST_DIR = unity
UNITY_BUILD_HOME = $(UNITY_HOE)/extras/fixture/build

UNITY_CFLAGS += -DUNITY_OUTPUT_CHAR=UnityOutputCharSpy_OutputChar
UNITY_WARNINGFLAGS = -Wall -Werror -Wswitch-default
#UNITY_WARNINGFLAGS += -Wshadow 

SRC_DIRS = \
  $(PROJECT_HOME_DIR)/src

TEST_SRC_DIRS = \
  $(PROJECT_TEST_DIR)\
  $(UNITY_HOME)/src\
  $(UNITY_HOME)/extras/fixture/src\
  $(UNITY_HOME)/extras/fixture/test\

TEST_SRC_FILES = \
 $(SRC_DIRS)/*.c\
 $(PROJECT_TEST_DIR)/*.c\
 $(PROJECT_TEST_DIR)/CircularBuffer/*.c\
 $(UNITY_HOME)/src/*.c\
 $(UNITY_HOME)/extras/fixture/src/*.c\
 $(UNITY_HOME)/extras/fixture/test/*.c

MOCKS_SRC_DIRS = \
  $(PROJECT_TEST_DIR)/mocks\

INCLUDE_DIRS =\
 -I.\
 -I./src\
 -I$(UNITY_HOME)/src\
 -I$(UNITY_HOME)/test\
 -I$(UNITY_HOME)/extras/fixture/src\
 -I$(UNITY_HOME)/extras/fixture/test\
 -I$(PROJECT_HOME_DIR)/mocks\

# include $(UNITY_BUILD_HOME)/MakefileWorker.mk

all: default

default:
 $(CC) $(UNITY_CFLAGS) $(UNITY_WARNINGFLAGS) $(INCLUDE_DIRS) \
 $(TEST_SRC_FILES) -o $(TARGET1) && ./$(TARGET1)

CircularBufferTest.c

#include "unity_fixture.h"

#include "CircularBuffer.h"

TEST_GROUP(CircularBuffer);

TEST_SETUP(CircularBuffer) {}
TEST_TEAR_DOWN(CircularBuffer) {}

TEST(CircularBuffer, CircularBufferCreate) {
  size_t len = 128;
  CircularBuffer cb = CircularBufferCreate(len);
  TEST_ASSERT_EQUAL_INT(0, cb.first);
  TEST_ASSERT_EQUAL_INT(0, cb.last);
}

TEST_GROUP_RUNNER(CircularBuffer) {
  RUN_TEST_CASE(CircularBuffer, CircularBufferCreate);
}

CircularBuffer.h

#pragma once
/** \file  */

#include <stdlib.h> // size_t

typedef struct CircularBuffer {
  size_t len;
  char *buf;
  size_t first;
  size_t last;
} CircularBuffer;

CircularBuffer CircularBufferCreate(size_t len);

CircularBuffer.c

/** \file  */
#include "CircularBuffer.h"

CircularBuffer CircularBufferCreate(size_t len) {
  char *buf = malloc(sizeof(char) * len);
  return (CircularBuffer){.len = len, .buf = buf, .first = 0, .last=0};
}

入出力結果(Terminal)

$ make -s
unity/CircularBuffer/CircularBufferTest.c:12:3: error: use of undeclared
      identifier 'CircularBuffer'
  CircularBuffer cb = CircularBufferCreate(len);
  ^
unity/CircularBuffer/CircularBufferTest.c:13:28: error: use of undeclared
      identifier 'cb'
  TEST_ASSERT_EQUAL_INT(0, cb.first);
                           ^
unity/CircularBuffer/CircularBufferTest.c:14:28: error: use of undeclared
      identifier 'cb'
  TEST_ASSERT_EQUAL_INT(0, cb.last);
                           ^
3 errors generated.
make: *** [default] Error 1
$ make -s
Undefined symbols for architecture x86_64:
  "_CircularBufferCreate", referenced from:
      _TEST_CircularBuffer_CircularBufferCreate_ in CircularBufferTest-695a18.o
     (maybe you meant: _TEST_CircularBuffer_CircularBufferCreate_run, _TEST_CircularBuffer_CircularBufferCreate_ )
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [default] Error 1
$ make -s
Unity test run 1 of 1
.

-----------------------
1 Tests 0 Failures 0 Ignored 
OK
$ 

0 コメント:

コメントを投稿

Comments on Google+: