在我的项目中,我们有一些单身人士,这在单元测试中往往存在问题。所以我想找到问题的解决方案。以下是我到目前为止所提供的内容:
smart_singleton.h
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CircleMenu circleMenu = (CircleMenu) findViewById(R.id.circle_menu);
circleMenu.setMainMenu(Color.parseColor("#CDCDCD"), R.mipmap.ic_launcher, R.mipmap.ic_launcher);
circleMenu.addSubMenu(Color.parseColor("#258CFF"), R.mipmap.ic_launcher)
.addSubMenu(Color.parseColor("#30A400"), R.mipmap.ic_launcher)
.addSubMenu(Color.parseColor("#FF4B32"), R.mipmap.ic_launcher)
.addSubMenu(Color.parseColor("#8A39FF"), R.mipmap.ic_launcher)
.addSubMenu(Color.parseColor("#FF6A00"), R.mipmap.ic_launcher);
circleMenu.setOnMenuSelectedListener(new OnMenuSelectedListener() {
@Override
public void onMenuSelected(int index) {
switch (index) {
case 0:
Intent intent = new Intent(MainActivity.this,Jude.class);
startActivity(intent);
break;
case 1:
Intent intent2 = new Intent(MainActivity.this,Jude.class);
startActivity(intent2);
break;
case 2:
Intent inten3 = new Intent(MainActivity.this,Jude.class);
startActivity(inten3);
break;
case 3:
Toast.makeText(MainActivity.this, "Settings button Clcked", Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(MainActivity.this, "GPS button Clicked", Toast.LENGTH_SHORT).show();
break;
}
}
}
);
circleMenu.setOnMenuStatusChangeListener(new OnMenuStatusChangeListener() {
@Override
public void onMenuOpened() {
Toast.makeText(MainActivity.this, "Menu Opend", Toast.LENGTH_SHORT).show();
}
@Override
public void onMenuClosed() {
Toast.makeText(MainActivity.this, "Menu Closed", Toast.LENGTH_SHORT).show();
}
}
);
}
}
smart_singleton.cpp
class smart_singleton
{
public:
static std::shared_ptr<smart_singleton> get_instance();
private:
smart_singleton();
smart_singleton(const smart_singleton&) = delete;
smart_singleton operator=(const smart_singleton&) = delete;
smart_singleton(smart_singleton&&) = default;
smart_singleton& operator=(smart_singleton&&) = default;
static std::weak_ptr<smart_singleton> weak_instance;
};
不同之处在于您需要在代码中的任何位置保存一个“get_instance()”中的shared_ptr,以使对象不被销毁。在您的生产代码中,它将位于main函数中的某个位置(或者是整个main范围内的某个对象)。在UT中,这将是一次测试的持续时间。
我希望得到一些反馈,你在这样的实现中发现了什么缺陷