aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/TCC/Tango.TCC.LoadTestLib/EigenDir/test/exceptions.cpp
blob: b83fb82ba6027a73e7441d12c00ffd1b3d03d169 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2011 Gael Guennebaud <gael.guennebaud@inria.fr>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.


// Various sanity tests with exceptions:
//  - no memory leak when a custom scalar type trow an exceptions
//  - todo: complete the list of tests!

#define EIGEN_STACK_ALLOCATION_LIMIT 100000000

#include "main.h"

struct my_exception
{
  my_exception() {}
  ~my_exception() {}
};
    
class ScalarWithExceptions
{
  public:
    ScalarWithExceptions() { init(); }
    ScalarWithExceptions(const float& _v) { init(); *v = _v; }
    ScalarWithExceptions(const ScalarWithExceptions& other) { init(); *v = *(other.v); }
    ~ScalarWithExceptions() {
      delete v;
      instances--;
    }

    void init() {
      v = new float;
      instances++;
    }

    ScalarWithExceptions operator+(const ScalarWithExceptions& other) const
    {
      countdown--;
      if(countdown<=0)
        throw my_exception();
      return ScalarWithExceptions(*v+*other.v);
    }
    
    ScalarWithExceptions operator-(const ScalarWithExceptions& other) const
    { return ScalarWithExceptions(*v-*other.v); }
    
    ScalarWithExceptions operator*(const ScalarWithExceptions& other) const
    { return ScalarWithExceptions((*v)*(*other.v)); }
    
    ScalarWithExceptions& operator+=(const ScalarWithExceptions& other)
    { *v+=*other.v; return *this; }
    ScalarWithExceptions& operator-=(const ScalarWithExceptions& other)
    { *v-=*other.v; return *this; }
    ScalarWithExceptions& operator=(const ScalarWithExceptions& other)
    { *v = *(other.v); return *this; }
  
    bool operator==(const ScalarWithExceptions& other) const
    { return *v==*other.v; }
    bool operator!=(const ScalarWithExceptions& other) const
    { return *v!=*other.v; }
    
    float* v;
    static int instances;
    static int countdown;
};

ScalarWithExceptions real(const ScalarWithExceptions &x) { return x; }
ScalarWithExceptions imag(const ScalarWithExceptions & ) { return 0; }
ScalarWithExceptions conj(const ScalarWithExceptions &x) { return x; }

int ScalarWithExceptions::instances = 0;
int ScalarWithExceptions::countdown = 0;


#define CHECK_MEMLEAK(OP) {                                 \
    ScalarWithExceptions::countdown = 100;                  \
    int before = ScalarWithExceptions::instances;           \
    bool exception_thrown = false;                         \
    try { OP; }                              \
    catch (my_exception) {                                  \
      exception_thrown = true;                              \
      VERIFY(ScalarWithExceptions::instances==before && "memory leak detected in " && EIGEN_MAKESTRING(OP)); \
    } \
    VERIFY(exception_thrown && " no exception thrown in " && EIGEN_MAKESTRING(OP)); \
  }

void memoryleak()
{
  typedef Eigen::Matrix<ScalarWithExceptions,Dynamic,1> VectorType;
  typedef Eigen::Matrix<ScalarWithExceptions,Dynamic,Dynamic> MatrixType;
  
  {
    int n = 50;
    VectorType v0(n), v1(n);
    MatrixType m0(n,n), m1(n,n), m2(n,n);
    v0.setOnes(); v1.setOnes();
    m0.setOnes(); m1.setOnes(); m2.setOnes();
    CHECK_MEMLEAK(v0 = m0 * m1 * v1);
    CHECK_MEMLEAK(m2 = m0 * m1 * m2);
    CHECK_MEMLEAK((v0+v1).dot(v0+v1));
  }
  VERIFY(ScalarWithExceptions::instances==0 && "global memory leak detected in " && EIGEN_MAKESTRING(OP)); \
}

void test_exceptions()
{
  CALL_SUBTEST( memoryleak() );
}
pan>set { _log = value; RaisePropertyChangedAuto(); } } private MachineSetupStates _state; /// <summary> /// Gets or sets the state. /// </summary> public MachineSetupStates State { get { return _state; } set { _state = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } private String _failedError; /// <summary> /// Gets or sets the setup failed error. /// </summary> public String FailedError { get { return _failedError; } set { _failedError = value; RaisePropertyChangedAuto(); } } /// <summary> /// Gets or sets the available time zones. /// </summary> public List<TimeZoneInfo> TimeZones { get; set; } private TimeZoneInfo _selectedTimeZone; /// <summary> /// Gets or sets the selected time zone. /// </summary> public TimeZoneInfo SelectedTimeZone { get { return _selectedTimeZone; } set { _selectedTimeZone = value; RaisePropertyChangedAuto(); } } private DeploymentSlot _deploymentSlot; /// <summary> /// Gets or sets the deployment slot. /// </summary> public DeploymentSlot DeploymentSlot { get { return _deploymentSlot; } set { _deploymentSlot = value; RaisePropertyChangedAuto(); } } private MachineSetupView _currentView; public MachineSetupView CurrentView { get { return _currentView; } set { _currentView = value; RaisePropertyChangedAuto(); } } #endregion #region Commands /// <summary> /// Gets or sets the complete command. /// </summary> public RelayCommand CompleteCommand { get; set; } /// <summary> /// Gets or sets the navigate to WiFi command. /// </summary> public RelayCommand NavigateToWiFiCommand { get; set; } /// <summary> /// Gets or sets the install command. /// </summary> public RelayCommand InstallCommand { get; set; } /// <summary> /// Gets or sets the restart command. /// </summary> public RelayCommand RestartCommand { get; set; } /// <summary> /// Gets or sets the time zone selected command. /// </summary> public RelayCommand TimeZoneSelectedCommand { get; set; } /// <summary> /// Gets or sets the environment selected command. /// </summary> public RelayCommand EnvironmentSelectedCommand { get; set; } #endregion #region Constructors /// <summary> /// Initializes a new instance of the <see cref="MachineSetupViewVM"/> class. /// </summary> /// <param name="applicationManager">The application manager.</param> /// <param name="machineSetupManager">The machine setup manager.</param> public MachineSetupViewVM(PPCWebClient ppcWebClient, IPPCApplicationManager applicationManager, IMachineSetupManager machineSetupManager, IOperationSystemManager operationSystemManager) { _ppcWebClient = ppcWebClient; _appManager = applicationManager; MachineSetupManager = machineSetupManager; DeploymentSlot = Settings.DeploymentSlot; SerialNumber = ""; CompleteCommand = new RelayCommand(CompleteSetup, () => State == MachineSetupStates.Completed); applicationManager.SetupRequired += ApplicationManager_SetupRequired; NavigateToWiFiCommand = new RelayCommand(EnsureWiFi); InstallCommand = new RelayCommand(Install); RestartCommand = new RelayCommand(() => { NavigateTo(MachineSetupView.WelcomeView); }); TimeZoneSelectedCommand = new RelayCommand(() => { NavigateTo(MachineSetupView.EnvironmentView); }); EnvironmentSelectedCommand = new RelayCommand(() => { NavigateTo(MachineSetupView.SetupWelcomeView); Settings.DeploymentSlot = DeploymentSlot; HostAddress = Settings.GetMachineServiceAddress(); Settings.Save(); }); _operationSystemManager = operationSystemManager; TimeZones = _operationSystemManager.GetAvailableTimeZones().ToList(); SelectedTimeZone = TimeZones.SingleOrDefault(x => x.ToString() == "(UTC+02:00) Jerusalem"); } #endregion #region Event Handlers /// <summary> /// Handles the SetupRequired event of the ApplicationManager. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> private void ApplicationManager_SetupRequired(object sender, EventArgs e) { var logger = new SimpleStringLogger(); logger.Categories.Remove(LogCategory.Debug); LogManager.RegisterLogger(logger); logger.LogReceived += Logger_LogReceived; LogManager.Log("SetupRequired event received. Navigating to MachineSetupView..."); NavigationManager.NavigateTo(NavigationView.MachineSetupView); } private void Logger_LogReceived(object sender, LogItemBase e) { AppendLog(e.Message); } #endregion #region Private Methods /// <summary> /// Appends the log. /// </summary> /// <param name="msg">The MSG.</param> private void AppendLog(String msg) { if (msg != null && !msg.Contains("SQL Examiner")) { InvokeUI(() => { View.AppendLog(msg + Environment.NewLine); }); } } /// <summary> /// Completes the setup. /// </summary> private void CompleteSetup() { LogManager.Log("Completing machine setup..."); String updater_exe = Path.Combine(_setup_result.UpdatePackagePath, "Tango.PPC.Updater.exe"); _appManager.UpdateApplication(updater_exe, PathHelper.GetStartupPath()); } /// <summary> /// Navigates to the specified view. /// </summary> /// <param name="view">The view.</param> private Task NavigateTo(MachineSetupView view) { CurrentView = view; return View.NavigateTo(view); } #endregion #region WiFi private async void EnsureWiFi() { await NavigateTo(MachineSetupView.WiFiTestView); await Task.Delay(2000); bool connected = await ConnectivityProvider.CheckInternetConnection(); if (connected) { if (BuildProvider.IsEureka) { await NavigateTo(MachineSetupView.EnvironmentView); } else { await NavigateTo(MachineSetupView.TimeZoneView); } } else { await NavigateTo(MachineSetupView.WiFiSelectionView); await ConnectivityProvider.RefreshAvailableWiFiNetworks(); ConnectivityProvider.ConnectionStateChanged += ConnectivityProvider_ConnectionStateChanged; } } private async void ConnectivityProvider_ConnectionStateChanged(object sender, Common.Connectivity.ConnectionStateEventArgs e) { if (e.IsConnected) { ConnectivityProvider.ConnectionStateChanged -= ConnectivityProvider_ConnectionStateChanged; await NavigateTo(MachineSetupView.WiFiTestView); await Task.Delay(5000); EnsureWiFi(); } } #endregion #region Setup private async void Install() { await NavigateTo(MachineSetupView.SetupProgressView); LogManager.Log("Starting machine setup..."); State = MachineSetupStates.Working; try { if (!App.StartupArgs.Contains("-webDebug")) { _ppcWebClient.Environment = DeploymentSlot; } if (!BuildProvider.IsEureka) { await _operationSystemManager.ChangeTimeZone(SelectedTimeZone); } _setup_result = await MachineSetupManager.Setup(SerialNumber); State = MachineSetupStates.Completed; LogManager.Log("Machine setup completed."); await NavigateTo(MachineSetupView.SetupCompletedView); } catch (Exception ex) { LogManager.Log(ex, "Machine setup failed."); FailedError = ex.FlattenMessage(); State = MachineSetupStates.Failed; await NavigateTo(MachineSetupView.SetupFailedView); } } #endregion } }